DocScoDocTest/tools/trac2md.py

70 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Translate Trac wiki markup to Markdown
Emmanuel Viennet for ScoDoc, Sept 2020
Nota: far from complete, based on a set of regexps
tables are *not translated*.
= ... =
== ... ==
=== ... ===
====
''...'' : *...*
"""
import sys
import re
SRC = sys.argv[1]
f = open(SRC, encoding="utf-8")
data = f.read()
EXPRS = (
# Attachements: [[attachment:...]]
(r'\[\[attachment:(?P<att>.*?)\]\]', r'<a class="attachment" href="/attachments/\g<att>" download>\g<att></a>', 0),
(r'^====(.*?)====\s*\n', r'\n#### \1\n', re.MULTILINE),
(r'^===(.*?)===\s*\n', r'\n### \1\n', re.MULTILINE),
(r'^==(.*?)==\s*\n', r'\n## \1\n', re.MULTILINE),
(r'^=(.*?)=\s*\n', r'\n# \1\n', re.MULTILINE),
(r'^====([^=]*?)$', r'#### \1', re.MULTILINE),
(r'^===([^=]*?)$', r'### \1', re.MULTILINE),
(r'^==([^=]*?)$', r'## \1', re.MULTILINE),
(r'^=([^=]*?)$', r'# \1', re.MULTILINE),
# Links: [[...]]
(r'\[\[([A-Za-z0-9\s.:/]*?)\]\]', r'[\1](\1.md)', 0),
# Links [[ url | text ]]
(r"\[\[(?P<url>http(s)?://[A-Za-z0-9\s.:/-]*?)\|(?P<txt>[\w\s-]*)\]\]", r'[\g<txt>](\g<url>)', 0), # external link
(r"\[\[([A-Za-z0-9\s.:/-]*?)\|([\w\s-]*)\]\]", r'[\2](\1.md)', 0), # internal (.md)
# Les references intrawiki PascalCased (upper CamelCase)
(r'\s([A-Z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*)', r' [\1](\1.md)', 0),
# Pour Sco``Doc
(r'\`\`', '', 0),
# Blocks
(r'\{\{\{(.*?)\}\}\}', r'```\1```', re.DOTALL|re.MULTILINE), # {{{...}}}
# Images (dans /trunk/doc/images/ ou /branches/ScoDoc7/doc/images/)
(r'\[\[Image\(source:/.*?/doc/images/(?P<im>.*?)(,.*?)?\)\]\]', r'![\g<im>](screens/\g<im>)', 0),
# Tables
(r'^\|\|(.*?)\|\|(.*?)\|\|$', r'\1|\2', re.MULTILINE),
# /!\
(r'/!\\', r'<img src="/img/alert.png" style="vertical-align: bottom; margin:0 0 0 0;" alt="/!\" />', 0),
# bolds & italics:
(r"'''(.*?)'''", r'**\1**', re.DOTALL), # '''...''' bold
(r'\'\'(.*?)\'\'', r'*\1*', re.DOTALL), # ''...''
(r'\s//(.*?)//', r'*\1*', 0), # //...//
)
for pattern, subst, flags in EXPRS:
data = re.sub( pattern, subst, data, flags=flags )
print(data)