Debian Codegraph @

documentation/source/makepsmd.py

raw
1 #!/usr/bin/env python
2 '''Process font documentation .md files for use on product sites '''
3 __url__ = 'https://github.com/silnrsi/fontdocs'
4 __copyright__ = 'Copyright (c) 2021-2023 SIL International (https://www.sil.org)'
5 __license__ = 'Released under the MIT License (http://opensource.org/licenses/MIT)'
6 __author__ = 'Victor Gaultney'
7
8 import re
9 from silfont.core import execute
10
11 argspec = [
12 ('infile', {'help': 'Input .md filename'}, {'type': 'infile'}),
13 ('outfile',{'help': 'Output .md filename'}, {'type': 'outfile'})
14 ]
15
16 def doit(args):
17 infile = args.infile
18 outfile = args.outfile
19
20 firstline = True
21 inheader = False
22 inpsonly = False
23 infontsc = False
24
25 classes = []
26
27 temptext = ""
28
29 # remove YAML header and uncomment the [font] shortcode
30 for line in infile:
31 if firstline:
32 firstline = False
33 inheader = True
34 continue
35 if inheader:
36 if line.startswith("---"):
37 inheader = False
38 continue
39 if line.startswith("<!-- PRODUCT SITE ONLY"):
40 inpsonly = True
41 continue
42 if inpsonly:
43 if line.startswith("-->"):
44 inpsonly = False
45 continue
46 if line.startswith("[font") and infontsc == False:
47 infontsc = True
48 fontscre = re.compile(r"\[font id='(\w+)'(.+)\]")
49 fontsc = fontscre.match(line)
50 fontscid = fontsc.group(1)
51 fontscend = fontsc.group(2)
52 temptext = temptext + line
53
54 # reconfigure image markup
55 mdimage = re.compile(r"!\[(.*?)\]\((\S+\.\w+)\)\{\.(\S+)\}\n?<!--\sPRODUCT\sSITE\sIMAGE\sSRC\s(\S+)\s-->")
56 temptext = mdimage.sub(r"<img class='\3' alt='\1' src='\4' />", temptext)
57 mdimagecap = re.compile(r"<figcaption>(.*?)<\/figcaption>")
58 temptext = mdimagecap.sub(r"[caption]<em>\1</em>[/caption]", temptext)
59
60 # replace local links with site references
61 temptext = temptext.replace(".md","")
62
63 # replace links to external markdown files
64 temptext = temptext.replace(".rawmd",".md")
65
66 # replace escaped @ in link texts
67 temptext = temptext.replace("[\@","[@")
68
69 # make a list of new classes needed for font shortcode
70 # separate searches for spans with 1, 2, or 3 settings
71 fontclass1 = re.compile(r"style='font-feature-settings:\s\"(\w+\d*)\"\s(\d+)'")
72 fontclass2 = re.compile(r"style='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'")
73 fontclass3 = re.compile(r"style='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'")
74 for match in fontclass1.finditer(temptext):
75 classes.append(("-" + match.group(1) + "-" + match.group(2), match.group(1) + " " + match.group(2)))
76 for match in fontclass2.finditer(temptext):
77 classes.append(("-" + match.group(1) + "-" + match.group(2) + "-" + match.group(3) + "-" + match.group(4), match.group(1) + " " + match.group(2) + ", " + match.group(3) + " " + match.group(4)))
78 for match in fontclass3.finditer(temptext):
79 classes.append(("-" + match.group(1) + "-" + match.group(2) + "-" + match.group(3) + "-" + match.group(4) + "-" + match.group(5) + "-" + match.group(6), match.group(1) + " " + match.group(2) + ", " + match.group(3) + " " + match.group(4) + ", " + match.group(5) + " " + match.group(6)))
80
81 # transform explicit font feature settings into classes
82 # separate searches for spans with 1, 2, or 3 settings
83 fontspan1 = re.compile(r"class='(\w+)+-(\w+)+\snormal'\sstyle='font-feature-settings:\s\"(\w+\d*)\"\s(\d+)'>")
84 fontspan2 = re.compile(r"class='(\w+)+-(\w+)+\snormal'\sstyle='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'>")
85 fontspan3 = re.compile(r"class='(\w+)+-(\w+)+\snormal'\sstyle='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'>")
86 temptext = fontspan1.sub(r"class='\1-\3-\4-\2 normal'>", temptext)
87 temptext = fontspan2.sub(r"class='\1-\3-\4-\5-\6-\2 normal'>", temptext)
88 temptext = fontspan3.sub(r"class='\1-\3-\4-\5-\6-\7-\8-\2 normal'>", temptext)
89
90 # add new font sortcodes for added classes
91 for c in classes:
92 temptext = temptext + "[font id='" + fontscid + c[0] + "'" + fontscend + " feats='" + c[1] + "']\n"
93
94 outfile.write(temptext)
95
96 return
97
98
99 def cmd() : execute(None,doit, argspec)
100 if __name__ == "__main__": cmd()