import argparse
import yaml
import sys
import re
from itertools import islice

parser = argparse.ArgumentParser()
parser.add_argument('source', help='Path to source file',
                              type=argparse.FileType('r'))
parser.add_argument('-o', '--output', type=argparse.FileType('w'),
                    default=sys.stdout, help='Output file')
parser.add_argument('-l', '--layout', help='Page layout',
                    default='main')
parser.add_argument('-s', '--strip-header', help='Strip header',
                    action='store_true')
arguments = parser.parse_args()

PRE_START_REGEX = re.compile('^<pre><code class="override-lang ([a-z0-9_-]+)">(.*)')
def transliterate_markdown(src, dst):
    in_code_block = False
    if arguments.strip_header:
        src = islice(src, 3, None)
    for line in src:
        if line.startswith('~~~'):
            in_code_block = not in_code_block
            if in_code_block:
                dest_f.write('{% highlight python %}\n')
            else:
                dest_f.write('{% endhighlight %}\n')
        elif line.startswith('</code></pre>'):
            dst.write('{% endhighlight %}\n')
        else:
            ps = PRE_START_REGEX.match(line)
            if ps is not None:
                dst.write('{{% highlight {} %}}\n'.format(ps.group(1)))
                dst.write(ps.group(2))
                dst.write('\n')
            else:
                dst.write(line)

# Parse metadata
metadata = {}
MD_REGEX = re.compile('^//([A-Z_]+)\s*:\s*(.*)\s*$')
while True:
    md_line = arguments.source.readline()
    match = MD_REGEX.match(md_line)
    if match is None:
        break
    if match.group(2):
        metadata[match.group(1)] = match.group(2)

content_type = metadata.get('CONTENT_TYPE', 'markdown')
if content_type not in ('markdown', 'md'):
    print("Tool can only convert Markdown source.", file=sys.stdout)
    exit(1)

yaml_data = {'layout': arguments.layout}
yaml_data['title'] = metadata.get('TITLE', 'Website')
if 'DESCRIPTION' in metadata:
    yaml_data['description'] = metadata['DESCRIPTION']
arguments.output.write('---\n')
yaml.dump(yaml_data, arguments.output, default_flow_style=False)
arguments.output.write('---\n')
transliterate_markdown(arguments.source, arguments.output)


@@ -584,16 +584,159 @@
 args()%0A%0A
+def extract_language(line):%0A    elements = line.rstrip().split('.')%0A    if len(elements) %3E 1:%0A        return elements%5B-1%5D%0A    return 'python'%0A%0A
 PRE_STAR
@@ -1082,38 +1082,85 @@
                 
-dest_f
+language = extract_language(line)%0A                dst
 .write('%7B%25 highl
@@ -1164,22 +1164,32 @@
 ghlight 
-python
+' + language + '
  %25%7D%5Cn')%0A
@@ -1227,13 +1227,10 @@
    d
-e
 st
-_f
 .wri

Support picking out syntaxes of other languages