fiss

Friedel's Initialization and Service Supervision
Log | Files | Refs | LICENSE

make-docs.py (2801B)


      1 import sys
      2 import re
      3 
      4 if len(sys.argv) < 2:
      5     print("make-docs.py <template> [infile]")
      6     exit(1)
      7 
      8 temp_path = sys.argv[1]
      9 infile = sys.stdin
     10 if len(sys.argv) >= 3:
     11     infile = open(sys.argv[2])
     12 
     13 WIDTH = 80
     14 HEADER_CHAR = '='
     15 TITLE_CHAR = '-'
     16 HEADER_SUFFIX = "<span class=right><span id=toggle_dark onclick=toggle_dark()> turn the lights off </span> <a href=https://github.com/friedelschoen/fiss><img id=github alt=GitHub src=github-mark.svg /></a></span>"
     17 HEADER_TEMPLATE = "<span class=header><a class=title id=top href=#top>{text}</a><span class=right><span id=toggle_dark onclick=toggle_dark()> turn the lights on </span> <a href=https://github.com/friedelschoen/fiss><img id=github alt=GitHub src=github-mark.svg /></a></span></span>"
     18 TITLE_TEMPLATE = "<a class=title id={id} href=#{id}>{text}</a>"
     19 
     20 with open(temp_path) as temp:
     21     PREFIX, SUFFIX = temp.read().split('%%%', 1)
     22 
     23 def inline_convert(text):
     24     text = re.sub(r'\*(.+?)\*', r'<b>\1</b>', text)
     25     text = re.sub(r'_(.+?)_', r'<u>\1</u>', text)
     26     text = re.sub(r'\[(.*?)\]\((.*?)\)', r'<a href="\2">\1</a>', text)
     27     return text
     28 
     29 in_code = False
     30 in_list = None
     31 
     32 print(PREFIX)
     33 
     34 for line in infile:
     35     line = line.strip()
     36     
     37     # is control
     38     if line.startswith("@man"):
     39         pass
     40     elif line.startswith("@header"):
     41         _, text = line.split(" ", 1)
     42         print(HEADER_TEMPLATE.format(text=text))
     43         sys.stdout.write(HEADER_CHAR * WIDTH)
     44     elif line.startswith("@title"):
     45         _, id, text = line.split(" ", 2)
     46         print(TITLE_TEMPLATE.format(id=id, text=text))
     47         sys.stdout.write(TITLE_CHAR * WIDTH)
     48     elif line.startswith("@code"):
     49         width = WIDTH -2
     50         if in_list:
     51             width -= 2
     52         print('+' + '-' * width + '+')
     53         in_code = True
     54     elif line.startswith("@endcode"):
     55         width = WIDTH -2
     56         if in_list:
     57             width -= 2
     58         sys.stdout.write('+' + '-' * width + '+')
     59         in_code = False
     60     elif line.startswith("@list"):
     61         if ' ' in line:
     62             _, in_list = line.split(' ')
     63         else:
     64             in_list = '*'
     65         sys.stdout.write(f"<div class=list>{in_list} ")
     66     elif line.startswith("@endlist"):
     67         sys.stdout.write("</div>")
     68         in_list = None
     69     elif line == '~':
     70         print()
     71         
     72     elif in_code:
     73         padding = WIDTH - 4 - len(line)
     74         if in_list:
     75             padding -= 2
     76         if padding < 0:
     77             padding = 0
     78         print('| ' + line + ' ' * padding + ' |')
     79     elif line.endswith('~'):
     80         print(inline_convert(line[:-1]))
     81     elif line:
     82         sys.stdout.write(inline_convert(line) + ' ') 
     83     elif in_list: # is empty but in line
     84         sys.stdout.write(f"</div>\n<div class=list>{in_list} ")
     85     else: # is empty
     86         print('\n')
     87         
     88 print(SUFFIX)