Changeset 4428:579956cac764 in livinglogic.python.xist

Show
Ignore:
Timestamp:
03/21/11 13:03:11 (8 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Document doc2txt and change it to use stdin/stdout.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • MIGRATION.rst

    r4396 r4428  
    1515 
    1616        oracle["user/pwd@db"] 
     17 
     18Changes to scripts 
     19------------------ 
     20 
     21*   The script ``doc2txt`` now reads from ``stdin`` and writes to ``stdout`` 
     22    instead of requiring file names on the command line. 
    1723 
    1824 
  • NEWS.rst

    r4418 r4428  
    2727    Instead of a ``connect`` object, there are now three objects for each 
    2828    supported database (i.e. ``oracle``, ``sqlite`` and ``mysql``) 
     29 
     30*   The script ``doc2txt`` now reads from ``stdin`` and writes to ``stdout`` 
     31    instead of requiring file names on the command line. 
    2932 
    3033*   :mod:`ll.xist.ns.rest` now handles option lists. 
  • src/ll/xist/scripts/doc2txt.py

    r4427 r4428  
    1111 
    1212""" 
    13 Module that uses :func:`html.astext` to generate a text version of a doc fragment. 
     13``doc2txt`` is a script converts an XML files using XIST doc vocabulary (i.e. 
     14:mod:`ll.xist.ns.doc` into plain text (by using :func:`ll.xist.ns.html.astext`, 
     15which needs an installed elinks_. 
    1416 
    15 Usage:: 
     17    .. _elinks: http://elinks.or.cz/ 
    1618 
    17     doc2txt spam.xml spam.txt 
     19``doc2txt`` supports the following options: 
    1820 
    19 to generate ``spam.txt`` from ``spam.xml`` 
     21    ``-t``, ``--title`` 
     22        The title for the document 
     23 
     24    ``-w``, ``--width`` 
     25        The width of the formatted text output (default 72) 
     26 
     27The input is read from stdin and printed to stdout. 
     28 
     29Example 
     30------- 
     31 
     32The following generates ``spam.txt`` from ``spam.xml`` formatted to 80 columns:: 
     33 
     34    $ doc2txt <spam.xml >spam.txt -w80 
     35 
    2036""" 
    2137 
     
    3046 
    3147 
    32 def xsc2txt(infilename, outfilename, title, width): 
    33     e = parse.tree(parse.File(infilename), parse.SGMLOP(), parse.NS(doc), parse.Node(pool=xsc.docpool())) 
     48def xsc2txt(instream, outstream, title, width): 
     49    e = parse.tree(parse.Stream(instream), parse.SGMLOP(), parse.NS(doc), parse.Node(pool=xsc.docpool())) 
    3450 
    3551    if title is None: 
     
    4561    e = e.conv() 
    4662 
    47     with open(outfilename, "wb") as f: 
    48         f.write(html.astext(e, width=width)) 
     63    outstream.write(html.astext(e, width=width)) 
    4964 
    5065 
    5166def main(args=None): 
    52     p = argparse.ArgumentParser(description="Convert an XML file using the ll.xist.ns.doc namespace into plain text") 
    53     p.add_argument("source", help="input XML file") 
    54     p.add_argument("target", help="output plain text file") 
     67    p = argparse.ArgumentParser(description="Convert an XML file (on stdin) using the ll.xist.ns.doc namespace into plain text and print it (on stdout)") 
    5568    p.add_argument("-t", "--title", dest="title", help="Title for the document") 
    5669    p.add_argument("-w", "--width", dest="width", help="Width of the plain text output (default %(default)s)", type=int, default=72) 
     
    5871    args = p.parse_args() 
    5972 
    60     xsc2txt(args.source, args.target, args.title, args.width) 
     73    xsc2txt(sys.stdin, sys.stdout, args.title, args.width) 
    6174 
    6275