Changeset d6acf79


Ignore:
Timestamp:
2015-05-16T18:50:49Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
ad678a4
Parents:
9740ce9
Message:

genhelp.py: Comments!

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
    119import re
    220import xml.etree.ElementTree as ET
     
    624NORMALIZE_RE = re.compile(r"([^<>\s\t])[\s\t]+([^<>\s\t])")
    725
    8 def join(list):
    9     return ''.join([str(x) for x in list])
     26# Helpers
    1027
    1128def 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
    1236    x = NORMALIZE_RE.sub(r"\1 \2", x or '')
    1337    return x.replace("\n", "").replace("\t", "")
    1438
    15 def fix_tree(tag, lvl=''):
     39def join(list):
     40    """Turns any iterator into a string"""
     41    return ''.join([str(x) for x in list])
     42
     43def 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.
    1647    if tag.tag.count("XInclude"):
    1748        tag.tag = 'include'
    1849
    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
    2661    tag.text = normalize(tag.text)
    2762    tag.tail = normalize(tag.tail)
    2863
    29 def parse_file(filename, parent=None):
     64
     65# Main logic
     66
     67def process_file(filename, parent=None):
    3068    tree = ET.parse(open(filename)).getroot()
    3169    fix_tree(tree)
     
    3371
    3472def parse_tag(tag, parent):
     73    """Calls a tag_... function based on the tag name"""
     74
    3575    fun = globals()["tag_%s" % tag.tag.replace("-", "_")]
    3676    return join(fun(tag, parent))
     
    4484    yield tag.tail
    4585
     86
     87# Main tag handlers
     88
    4689def handle_subject(tag, parent):
     90    """Tag handler for preface, chapter, sect1 and sect2 (aliased below)"""
     91
    4792    yield '?%s\n' % tag.attrib['id']
    4893
     
    5297                           "command-list", "ircexample"]:
    5398            if not first:
     99                # Spaces between paragraphs
    54100                yield "\n"
    55101            first = False
     
    74120
    75121def handle_command(tag, prefix=''):
     122    """Tag handler for <bitlbee-command> (called from handle_subject)"""
     123
    76124    this_cmd = prefix + tag.attrib['name']
    77125
     
    95143
    96144def handle_setting(tag):
     145    """Tag handler for <bitlbee-setting> (called from handle_subject)"""
     146
    97147    yield "?set %s\n" % tag.attrib['name']
    98148    yield "\x02Type:\x02 %s\n" % tag.attrib["type"]
     
    109159    yield "\n%\n"
    110160
     161
     162# Aliases for tags that behave like subjects
    111163tag_preface = handle_subject
    112164tag_chapter = handle_subject
     
    114166tag_sect2 = handle_subject
    115167
     168# Aliases for tags that don't have any special behavior
    116169tag_ulink = parse_subtags
    117170tag_note = parse_subtags
     
    119172tag_ircexample = parse_subtags
    120173
     174
     175# Handlers for specific tags
     176
    121177def tag_include(tag, parent):
    122     return parse_file(tag.attrib['href'], tag)
     178    return process_file(tag.attrib['href'], tag)
    123179
    124180def tag_para(tag, parent):
     
    158214    yield '\n'
    159215
     216
    160217def main():
    161     txt = parse_file(IN_FILE)
     218    txt = process_file(IN_FILE)
    162219    open(OUT_FILE, "w").write(txt)
    163220
Note: See TracChangeset for help on using the changeset viewer.