Skip to content

Instantly share code, notes, and snippets.

@snopoke
Created February 5, 2015 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snopoke/39a062b334473b18ddb0 to your computer and use it in GitHub Desktop.
Save snopoke/39a062b334473b18ddb0 to your computer and use it in GitHub Desktop.
Creole to Markdown
import functools
import re
import sys
def header_repl(matchobj):
return "{} {}".format(len(matchobj.group(1)) * '#', matchobj.group(2))
def list_repl(matchobj, ordered=True):
level = len(matchobj.group(1))
tmpl = '{}{}'.format(' ' * (level - 1), '1.' if ordered else '*')
return tmpl
patterns = [
(re.compile('\{\{\{(.*?)\}\}\}'), r'`\1`'),
(re.compile('\{\{\{\s*.*?[!#]{2}', re.MULTILINE), r'```'),
(re.compile('^[ ]*(#+)', re.MULTILINE), list_repl),
(re.compile('^[ ]*(\*{2,})', re.MULTILINE), functools.partial(list_repl, ordered=False)),
(re.compile('\{\{\{', re.MULTILINE), r'```'),
(re.compile('\}\}\}'), r'```'),
(re.compile('^[ ]*(=+)\s*([^=]+?)\s*=+', re.MULTILINE), header_repl),
(re.compile('//(.*)//'), r'*\1*'),
]
def creole_to_markdown(data):
"""
Convert creole markup (http://en.wikipedia.org/wiki/Creole_(markup)) to markdown.
Does not support tables.
>>> creole_to_markdown("some //italics//")
'some *italics*'
>>> print creole_to_markdown('''# A
... # B
... ## C
... ### D
... # E''')
1. A
1. B
1. C
1. D
1. E
>>> print creole_to_markdown('''* A
... * B
... ** C
... *** D
... * E''')
* A
* B
* C
* D
* E
>>> print creole_to_markdown('''{{{
... !#xml
... <some><xml></xml></some>
... }}}
...
... text
...
... {{{
... random code block without syntax formatting
... }}}
... {{{
... #!xml
... <morexml></morexml>
... }}}''')
```xml
<some><xml></xml></some>
```
<BLANKLINE>
text
<BLANKLINE>
```
random code block without syntax formatting
```
```xml
<morexml></morexml>
```
>>> print creole_to_markdown('''text
...
... {{{
... random code block without syntax formatting
... }}}''')
text
<BLANKLINE>
```
random code block without syntax formatting
```
>>> creole_to_markdown("* {{{inline code}}}")
'* `inline code`'
>>> print creole_to_markdown('''= Header 1 =
... text
... == Header 2==
... === Header 3 ===''')
# Header 1
text
## Header 2
### Header 3
>>> creole_to_markdown('{{{valid}}} and {{{expires}}}')
'`valid` and `expires`'
"""
for pattern, repl in patterns:
data = pattern.sub(repl, data)
return data
if __name__ == '__main__':
file_name = sys.argv[1]
with open(file_name) as f:
data = f.read()
f.close()
sys.stdout.write(creole_to_markdown(data))
@snopoke
Copy link
Author

snopoke commented Feb 5, 2015

More robust version here but no markdown support yet: https://bitbucket.org/thesheep/wikicreole/wiki/Home

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment