Skip to content

Instantly share code, notes, and snippets.

@tos-kamiya
Last active October 17, 2022 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tos-kamiya/4783246fb6a463ea160d332e001d07fc to your computer and use it in GitHub Desktop.
Save tos-kamiya/4783246fb6a463ea160d332e001d07fc to your computer and use it in GitHub Desktop.
コードブロックやテーブルを含むMarkdown文書を装飾をそれなりに残しつつMoodleのエディタに貼り付けるためのスクリプト(手抜き版)
#!/usr/bin/env python3
import sys
from bs4 import BeautifulSoup
from markdown import markdown
from docopt import docopt
__doc__ = '''Convert markdown to Moodle-ish html
Usage:
to_moodle_html [<inputmd>]
'''
args = docopt(__doc__, sys.argv[1:])
if args.inputmd:
with open(args.inputmd, 'r', encoding='utf-8') as inp:
text = inp.read()
else:
text = sys.stdin.read()
html = markdown(text, extensions=['fenced_code', 'tables'])
soup = BeautifulSoup(html, 'html.parser')
tables = soup.find_all("table")
for t in tables:
t.attrs['border'] = '1'
pres = soup.find_all('pre')
for t in pres:
s = t.attrs.setdefault('style', '')
s = s.rstrip()
if s and not s.endswith(';'):
s = s + ';'
t.attrs['style'] = s + 'background-color: #e8e8e8; padding: 10px;'
print(soup.prettify())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment