Debian Codegraph @

texmf-dist/doc/fonts/gentium-sil/Gentium/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-2025 SIL Global (https://www.sil.org)'
5 __license__ = 'Released under the MIT License (https://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 mdlink = re.compile(r"\((?!https://)(?P<path>[^()]*).md\)")
62 temptext = mdlink.sub(r"(\g<path>)",temptext)
63
64 # replace links to external markdown files
65 temptext = temptext.replace(".rawmd",".md")
66
67 # replace escaped @ in link texts
68 temptext = temptext.replace(r"[\@",r"[@")
69
70 # make a list of new classes needed for font shortcode
71 # separate searches for spans with 1, 2, or 3 settings
72 fontclass1 = re.compile(r"style='font-feature-settings:\s\"(\w+\d*)\"\s(\d+)'")
73 fontclass2 = re.compile(r"style='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'")
74 fontclass3 = re.compile(r"style='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'")
75 for match in fontclass1.finditer(temptext):
76 classes.append(("-" + match.group(1) + "-" + match.group(2), match.group(1) + " " + match.group(2)))
77 for match in fontclass2.finditer(temptext):
78 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)))
79 for match in fontclass3.finditer(temptext):
80 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)))
81
82 # transform explicit font feature settings into classes
83 # separate searches for spans with 1, 2, or 3 settings
84 fontspan1 = re.compile(r"class='(\w+)+-(\w+)+\snormal'\sstyle='font-feature-settings:\s\"(\w+\d*)\"\s(\d+)'>")
85 fontspan2 = re.compile(r"class='(\w+)+-(\w+)+\snormal'\sstyle='font-feature-settings:\s\"(\w+\d*)\"\s(\d+),\s\"(\w+\d*)\"\s(\d+)'>")
86 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+)'>")
87 temptext = fontspan1.sub(r"class='\1-\3-\4-\2 normal'>", temptext)
88 temptext = fontspan2.sub(r"class='\1-\3-\4-\5-\6-\2 normal'>", temptext)
89 temptext = fontspan3.sub(r"class='\1-\3-\4-\5-\6-\7-\8-\2 normal'>", temptext)
90
91 # add new font sortcodes for added classes
92 for c in classes:
93 temptext = temptext + "[font id='" + fontscid + c[0] + "'" + fontscend + " feats='" + c[1] + "']\n"
94
95 outfile.write(temptext)
96
97 return
98
99
100 def cmd() : execute(None,doit, argspec)
101 if __name__ == "__main__": cmd()