Changeset d6acf79
- Timestamp:
- 2015-05-16T18:50:49Z (9 years ago)
- Branches:
- master
- Children:
- ad678a4
- Parents:
- 9740ce9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user-guide/genhelp.py
r9740ce9 rd6acf79 1 #!/usr/bin/env python 2 # 3 # This program is free software; you can redistribute it and/or 4 # modify it under the terms of the GNU General Public License 5 # as published by the Free Software Foundation; either version 2 6 # of the License, or (at your option) any later version. 7 # 8 # This program is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 # GNU General Public License for more details. 12 # 13 # You should have received a copy of the GNU General Public License 14 # along with this program; if not, write to the Free Software 15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 16 # Boston, MA 02110-1301, USA. 17 18 1 19 import re 2 20 import xml.etree.ElementTree as ET … … 6 24 NORMALIZE_RE = re.compile(r"([^<>\s\t])[\s\t]+([^<>\s\t])") 7 25 8 def join(list): 9 return ''.join([str(x) for x in list]) 26 # Helpers 10 27 11 28 def normalize(x): 29 """Normalize whitespace of a string. 30 31 The regexp turns any sequence of whitespace into a single space if it's in 32 the middle of the tag text, and then all newlines and tabs are removed, 33 keeping spaces. 34 """ 35 12 36 x = NORMALIZE_RE.sub(r"\1 \2", x or '') 13 37 return x.replace("\n", "").replace("\t", "") 14 38 15 def fix_tree(tag, lvl=''): 39 def join(list): 40 """Turns any iterator into a string""" 41 return ''.join([str(x) for x in list]) 42 43 def fix_tree(tag, debug=False, lvl=''): 44 """Walks the XML tree and modifies it in-place fixing various details""" 45 46 # The include tags have an ugly namespace in the tag name. Simplify that. 16 47 if tag.tag.count("XInclude"): 17 48 tag.tag = 'include' 18 49 19 #print("%s<%s>%r" % (lvl, tag.tag, [tag.text, normalize(tag.text)])) 20 21 for subtag in tag: 22 fix_tree(subtag, lvl + " ") 23 24 #print("%s</%s>%r" % (lvl, tag.tag, [tag.tail, normalize(tag.tail)])) 25 50 # Print a pretty tree-like representation of the processed tags 51 if debug: 52 print("%s<%s>%r" % (lvl, tag.tag, [tag.text, normalize(tag.text)])) 53 54 for subtag in tag: 55 fix_tree(subtag, debug, lvl + " ") 56 57 if debug: 58 print("%s</%s>%r" % (lvl, tag.tag, [tag.tail, normalize(tag.tail)])) 59 60 # Actually normalize whitespace 26 61 tag.text = normalize(tag.text) 27 62 tag.tail = normalize(tag.tail) 28 63 29 def parse_file(filename, parent=None): 64 65 # Main logic 66 67 def process_file(filename, parent=None): 30 68 tree = ET.parse(open(filename)).getroot() 31 69 fix_tree(tree) … … 33 71 34 72 def parse_tag(tag, parent): 73 """Calls a tag_... function based on the tag name""" 74 35 75 fun = globals()["tag_%s" % tag.tag.replace("-", "_")] 36 76 return join(fun(tag, parent)) … … 44 84 yield tag.tail 45 85 86 87 # Main tag handlers 88 46 89 def handle_subject(tag, parent): 90 """Tag handler for preface, chapter, sect1 and sect2 (aliased below)""" 91 47 92 yield '?%s\n' % tag.attrib['id'] 48 93 … … 52 97 "command-list", "ircexample"]: 53 98 if not first: 99 # Spaces between paragraphs 54 100 yield "\n" 55 101 first = False … … 74 120 75 121 def handle_command(tag, prefix=''): 122 """Tag handler for <bitlbee-command> (called from handle_subject)""" 123 76 124 this_cmd = prefix + tag.attrib['name'] 77 125 … … 95 143 96 144 def handle_setting(tag): 145 """Tag handler for <bitlbee-setting> (called from handle_subject)""" 146 97 147 yield "?set %s\n" % tag.attrib['name'] 98 148 yield "\x02Type:\x02 %s\n" % tag.attrib["type"] … … 109 159 yield "\n%\n" 110 160 161 162 # Aliases for tags that behave like subjects 111 163 tag_preface = handle_subject 112 164 tag_chapter = handle_subject … … 114 166 tag_sect2 = handle_subject 115 167 168 # Aliases for tags that don't have any special behavior 116 169 tag_ulink = parse_subtags 117 170 tag_note = parse_subtags … … 119 172 tag_ircexample = parse_subtags 120 173 174 175 # Handlers for specific tags 176 121 177 def tag_include(tag, parent): 122 return p arse_file(tag.attrib['href'], tag)178 return process_file(tag.attrib['href'], tag) 123 179 124 180 def tag_para(tag, parent): … … 158 214 yield '\n' 159 215 216 160 217 def main(): 161 txt = p arse_file(IN_FILE)218 txt = process_file(IN_FILE) 162 219 open(OUT_FILE, "w").write(txt) 163 220
Note: See TracChangeset
for help on using the changeset viewer.