fork/mons
git clone https://git.julia.blue/fork/mons
17b9303
main
1import textwrap 2 3from docutils import nodes 4from docutils .statemachine import StringList 5from sphinx .application import Sphinx 6from sphinx .domains .std import Glossary 7 8 9class NodeReprVisitor (nodes .NodeVisitor ): 10content_indent = " " 11 12def __init__ (self ,document ): 13super ().__init__ (document ) 14self .content = "" 15self .indent = 0 16 17def visit_bullet_list (self ,node ): 18self .bullet = node ["bullet" ] 19self .content += "\n" 20self .indent += 1 21 22def depart_bullet_list (self ,node ): 23self .content += "\n" 24self .indent -= 1 25 26def visit_list_item (self ,node ): 27self .content += f" { self . content_indent * self . indent } { self . bullet } " 28 29def visit_paragraph (self ,node ): 30self .content += node .astext ()+ "\n\n" 31 32def unknown_visit (self ,node ): 33pass 34 35def unknown_departure (self ,node ): 36pass 37 38 39class GlossaryGen (Glossary ): 40def run (self )-> list [nodes .Node ]: 41def parse_content (node :nodes .Node ): 42assert node .document 43visitor = NodeReprVisitor (node .document ) 44node .walkabout (visitor ) 45return visitor .content 46 47node = nodes .Element () 48self .state .nested_parse ( 49self .content ,self .content_offset ,node ,match_titles = True 50 ) 51 52parsed_content = ["" ] 53for child in node .children : 54if len (child .children )== 2 : 55term ,content = child .children [:2 ] 56parsed_content .append (term .astext ()) 57parsed_content .append ( 58textwrap .indent (parse_content (content )," " )+ "\n" 59 ) 60 61content = "\n" .join (parsed_content ).splitlines () 62self .content = StringList (content ,content ) 63 64return super ().run () 65 66 67def setup (app :Sphinx ): 68app .add_directive ("glossarygen" ,GlossaryGen )