tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mm-converter.py (3867B)


      1 #!/usr/bin/env python3
      2 
      3 # set correct links (pandoc did not deal with github links properly)
      4 
      5 import os
      6 import re
      7 import subprocess
      8 
      9 regex_pdf_links1 = re.compile(r'`(.*)\<g3doc\/(.*)\.pdf\>`__', 
     10                            re.M | re.X) # Multiline and Verbose
     11 regex_md_links = re.compile(r'`(.*)\<g3doc\/(.*)\.md\>`__', 
     12                            re.M | re.X) # Multiline and Verbose
     13 regex_md_links2 = re.compile(r'`(.*)\n(.*)\<g3doc\/(.*)\.md\>`__', 
     14                            re.M | re.X) # Multiline and Verbose
     15 regex_pdf_links2 = re.compile(r'`(.*)\n\s+(.*)\<g3doc\/(.*)\.pdf\>`__', 
     16                            re.M | re.X) # Multiline and Verbose
     17 
     18 def remove_links_to_index2(data):
     19    # remove liks to the index, they are useless in py4web docs
     20    data = data
     21    print(re.search(regex_pdf_links2, data))
     22    return re.sub(regex_pdf_links2, 
     23                  r':download:`\1 \2<g3doc/\3.pdf>`',
     24                  data)
     25 
     26 def remove_links_to_index(data):
     27    # remove liks to the index, they are useless in py4web docs
     28    data = data
     29    print(re.search(regex_pdf_links1, data))
     30    return re.sub(regex_pdf_links1, 
     31                  r':download:`\1<g3doc/\2.pdf>`',
     32                  data)
     33 
     34 def rewrite_md_links(data):
     35    # remove liks to the index, they are useless in py4web docs
     36    data = data
     37    print(re.search(regex_md_links, data))
     38    data = re.sub(regex_md_links, 
     39                  r'`\1<\2.html>`__',
     40                  data)
     41    data = re.sub(regex_md_links2, 
     42                  r'`\1 \2<\3.html>`__',
     43                  data)
     44    return data
     45 
     46 
     47 docs_on_pages = [
     48    'README.md',
     49    'quick_reference.md',
     50    'design_philosophy.md',
     51    'impl_details.md',
     52    'faq.md',
     53    'release_testing_process.md'
     54 ]
     55 
     56 def convert2md(file):
     57    print(f"    Working on file {file}")
     58    file = os.path.join('g3doc', file)
     59    data = open(file, 'r').read()
     60    write_files(file, data)
     61    
     62 def write_files(file, data):
     63    for extension in ['rst']:
     64        ext_dir = os.getcwd()
     65        md_dir = os.path.join(os.getcwd(), 'g3doc')
     66        if not os.path.isdir(ext_dir):
     67            os.mkdir(ext_dir)
     68        ext_file = os.path.join(ext_dir , os.path.splitext(os.path.basename(file))[0] + "." + extension)
     69        md_file = os.path.join(md_dir , os.path.splitext(os.path.basename(file))[0] + ".md")
     70        print(f'writing {ext_file}')
     71        if os.path.exists(ext_file):
     72            os.unlink(ext_file)
     73        with open(ext_file, 'w') as handler:
     74            write_format(extension, ext_file, handler, md_file, data)
     75 
     76 
     77 def write_format(extension, ext_file, handler, md_file, data):
     78    if extension =='md':
     79            handler.write(data)
     80    elif extension =='rst':
     81        try:
     82            subprocess.call(['pandoc', '-s', md_file, '-f', 'markdown', '-t', 'rst', '-o', ext_file])
     83            data = open(ext_file, 'r').read() 
     84            data = remove_links_to_index(data)
     85            data = remove_links_to_index2(data)
     86            data = rewrite_md_links(data)
     87            handler.write(data)
     88            # Open a file for writing
     89            # with open('tmp.txt', 'w') as f:
     90                # Call the subprocess and redirect the output to the file
     91                # subprocess.call(['awk', '{ gsub(/<g3doc\//, "<"); print }', ext_file], stdout=f)
     92                # os.system('mv tmp.txt ' + ext_file)
     93 
     94        except FileNotFoundError:
     95            print("\n **** ERROR ****: you need the Pandoc module installed!")
     96            exit(0)
     97    elif extension =='html':
     98        try:
     99            subprocess.call(['pandoc', '-s', md_file, '-f', 'markdown', '-t', 'html', '-o', ext_file,  '--highlight-style=kate'])
    100        except FileNotFoundError:
    101            print("\n **** ERROR ****: you need the Pandoc module installed!")
    102            exit(0)
    103 
    104 
    105 if __name__ == "__main__":
    106    for doc in docs_on_pages:
    107        print(doc)
    108        convert2md(doc)