Skip to content

Instantly share code, notes, and snippets.

@craigphicks
Created March 10, 2019 00:40
Show Gist options
  • Save craigphicks/dde6f2d838196ae4e5eea7362c6ae828 to your computer and use it in GitHub Desktop.
Save craigphicks/dde6f2d838196ae4e5eea7362c6ae828 to your computer and use it in GitHub Desktop.
Read a yaml conf file with markdown extensions and use that to format python-markdown command line
import yaml
#import pdb
import sys
import subprocess
import pprint
fn_mdin = "./mdext.yml"
fn_mdcfgtmp = "./tmp/mdext-CFG.yml"
fn_scr="./call-pymd.out"
print ("This is the name of the script: {}".format(sys.argv[0]), file=sys.stderr)
print ("Number of arguments: {}".format(len(sys.argv)), file=sys.stderr)
print ("The arguments are: {}".format( str(sys.argv)), file=sys.stderr)
if len(sys.argv) < 1 :
print("at least md in/out filenames required", file=sys.stderr)
sys.exit(10)
with open(fn_mdin) as stream:
try:
dic = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
sys.exit(20)
########################################
cstr=""
for k1 in dic:
v1=dic[k1]
if v1 is None:
continue
cstr += k1
cstr += ":\n"
for k2 in v1:
cstr += " {}: {}\n".format(k2,dic[k1][k2])
with open(fn_mdcfgtmp, 'w') as out_file:
out_file.write(cstr);
print("wrote temp config file {}".format(fn_mdcfgtmp), file=sys.stderr)
########################################
args=["python", "-m", "markdown", "-c", fn_mdcfgtmp ]
for k1 in dic:
args.append("-x")
args.append(k1)
for i in range(1,len(sys.argv)):
args.append(sys.argv[i])
#pprint.pprint(args, stream=sys.stderr)
with open(fn_scr, 'w') as out_file:
out_file.write(" ".join(args));
print("wrote temp script file {}".format(fn_scr), file=sys.stderr)
#pdb.set_trace();
cp = subprocess.run(args,stdin=sys.stdin,stdout=sys.stdout,stderr=subprocess.PIPE,check=False)
if cp.returncode != 0 :
print(cp.stderr, file=sys.stderr)
else:
print("\nSuccess", file=sys.stderr)
#pdb.set_trace();
meta:
toc:
title: ToC
baselevel: 2
permalink: True
tables:
fenced_code:
footnotes:
attr_list:
def_list:
abbr:
admonition:
sane_lists:
pymdownx.betterem:
smart_enable: all
pymdownx.superfences:
pymdownx.extrarawhtml:
pymdownx.caret:
pymdownx.critic:
pymdownx.details:
#pymdownx.emoji:
# emoji_generator: !!python/name:pymdownx.emoji.to_svg
pymdownx.inlinehilite:
pymdownx.magiclink:
pymdownx.mark:
pymdownx.smartsymbols:
pymdownx.tasklist:
custom_checkbox: true
pymdownx.tilde:
mdx_include:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment