From 5e52622b266cf353f039f274ed8db934148f8039 Mon Sep 17 00:00:00 2001 From: Darks Date: Wed, 10 Feb 2021 22:18:02 +0100 Subject: [PATCH] First version of script Adds support for: - # H1 title - ## H2 title - ### H3 title - *emphasis text* - **strong text** - [link](https://domain.tld) - {{ tableofcontents }} --- md2bbc.py | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ sample.md | 17 +++++++++++ 2 files changed, 102 insertions(+) create mode 100755 md2bbc.py create mode 100644 sample.md diff --git a/md2bbc.py b/md2bbc.py new file mode 100755 index 0000000..b33c617 --- /dev/null +++ b/md2bbc.py @@ -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) diff --git a/sample.md b/sample.md new file mode 100644 index 0000000..fa44905 --- /dev/null +++ b/sample.md @@ -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