First version of script

Adds support for:
- # H1 title
- ## H2 title
- ### H3 title
- *emphasis text*
- **strong text**
- [link](https://domain.tld)
- {{ tableofcontents }}
This commit is contained in:
Darks 2021-02-10 22:18:02 +01:00
parent 3dc4b97fe3
commit 5e52622b26
Signed by: Darks
GPG Key ID: 7515644268BE1433
2 changed files with 102 additions and 0 deletions

85
md2bbc.py Executable file
View File

@ -0,0 +1,85 @@
#!/usr/bin/env python3
# Maintainer: Darks
# Disclaimer: this script has been done as a quick n' dirty way to convert
# markdown to Planète Casio's BBcode. As it, it does support only those
# features:
# - # H1 title
# - ## H2 title
# - ### H3 title
# - *emphasis text*
# - **strong text**
# - [link](https://domain.tld)
# - {{ tableofcontents }}
#
# More features may be added in the future, but this script is provided as it.
import argparse, re, sys
# Manage arguments
parser = argparse.ArgumentParser(
description="Convert Markdown to PC's BBcode")
parser.add_argument("input", type=argparse.FileType('r'), help="input file")
args = parser.parse_args()
# Useful regex
r_h1 = re.compile("^# (.+)$", re.I|re.M)
r_h2 = re.compile("^## (.+)$", re.I|re.M)
r_h3 = re.compile("^### (.+)$", re.I|re.M)
r_link = re.compile("(\W)\[(.+?)\]\((.+?)\)(\W)", re.I|re.M)
r_em = re.compile("(\s)\*(\w.+\w)\*(\s)", re.I|re.M)
r_strong = re.compile("(\s)\*\*(\w.+\w)\*\*(\s)", re.I|re.M)
# Open and extract document
markdown = args.input.read().splitlines()
# Manage titles
table_of_contents = ""
n_h1 = 0
n_h2 = 0
n_h3 = 0
output = []
# TODO: factorize this pile of shit
for line in markdown:
if (res := r_h1.search(line)):
n_h1 += 1
n_h2 = 0
n_h3 = 0
header = res.group(1)
index = f"{n_h1}"
table_of_contents += f"[target={index}]{index}{header}[/target]\n"
output.append(f"[big][brown][b]{index}{header}[/b][/brown][/big]"
f"[label={index}]")
elif (res := r_h2.search(line)):
n_h2 += 1
n_h3 = 0
header = res.group(1)
index = f"{n_h1}.{n_h2}"
table_of_contents += f"[target={index}]{index}{header}[/target]\n"
output.append(f"[brown][b]{index}{header}[/b][/brown]"
f"[label={index}]")
elif (res := r_h3.search(line)):
n_h3 += 1
header = res.group(1)
index = f"{n_h1}.{n_h2}.{n_h3}"
table_of_contents += f"[target={index}]{index}{header}[/target]\n"
output.append(f"[b]{index}{header}[/b]"
f"[label={index}]")
else:
res = r_link.sub(
lambda m: f"{m.group(1)}[url={m.group(3)}]{m.group(2)}[/url]{m.group(4)}", line)
res = r_strong.sub(
lambda m: f"{m.group(1)}[b]{m.group(2)}[/b]{m.group(3)}", res)
res = r_em.sub(
lambda m: f"{m.group(1)}[i]{m.group(2)}[/i]{m.group(3)}", res)
output.append(res)
# Add table of content
output = '\n'.join(output)
output = re.sub("{{ tableofcontents }}", table_of_contents, output)
# Print output on stdout
print(output)

17
sample.md Normal file
View File

@ -0,0 +1,17 @@
{{ tableofcontents }}
# Heading 1
## Heading 1.1
Some text
### Heading 1.1.1
Another **strong text** with *emphasis* and [a link](https://domain.tld) and [another one](https://domain.tld).
### Heading 1.1.2
## Heading 1.2
# Heading 2