Dear asciidoc users,

In my asciidoc documents I will replace each occurence of 
"pagenumber::original[2]" with the output of the attached python script. 
The python script standalone works as expected. But how could I tell 
asciidoc to call the python script if the value above will be found?

The attached config seems to be wrong. Because it is hard to understand, 
could ypu point me to a very good documentation how to configure such a 
filter? Otherwise could you please give me an explanation of an example?

If my asciidoc file has following text:

----------- snip ---------
pagenumber::original[2]

An ſeinen Ufern läuft das Schienenband,
Sich ſchwingend über kühn gewölbte Brücken;
----------- snap ---------

the docbook via "asciidoc -b docbook -f ./orig_pagenumber.conf" should be:

----------- snip ---------
<emphasis role="orig_pageno">2</emphasis>

An ſeinen Ufern läuft das Schienenband,
Sich ſchwingend über kühn gewölbte Brücken;
----------- snap ---------

Thanks in advance,

with best regards

Andreas

-- 
You received this message because you are subscribed to the Google Groups 
"asciidoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/asciidoc.
For more options, visit https://groups.google.com/d/optout.
#
# AsciiDoc code filter configuration file.
#
# Documented in code-filter-readme.txt
#
[pagenum-filter-style]
pagenum-style=
template=pagenum-paragraph
subs=()
filter='pagenumber.py -b {basebackend}'

[paradef-default]
template::[pagenum-filter-style]

[pagenum-paragraph]
template::[paradef-default]
#!/usr/bin/python
# -*- coding: utf8 -*-

'''
NAME
    orig_pagenumber - AsciiDoc filter to handle original page numbers in scanned books

SYNOPSIS
    orig_pagenumber -b backend
                [ --help | -h ] [ --version | -v ]

DESCRIPTION
    This filter reads source code from the standard input, processes orig pagenumabers and writes to the standard output.


OPTIONS
    --help, -h
        Print this documentation.

    -b
        Backend output file format: 'docbook', 'linuxdoc', 'html', 'css'.

    --version, -v
        Print program version number.

BUGS
    - not known yet

AUTHOR
    Written by Andreas Romeyke, <[email protected]>

URLS
    http://sourceforge.net/projects/asciidoc/
    http://www.methods.co.nz/asciidoc/

COPYING
    Copyright (C) 2013 Andreas Romeyke. Free use of this software is
    granted under the terms of the GNU General Public License (GPL).
'''

import os, sys, re, string

VERSION = '0.0.1'

# Globals.
backend = None
keywordtags = {
    'html':
        ('<strong class="orig_pageno">','</strong>'),
    'css':
        ('<strong>','</strong>'),
    'docbook':
        ('<emphasis role="orig_pageno">','</emphasis>'),
    'linuxdoc':
        ('','')
}

def print_stderr(line):
    sys.stderr.write(line+os.linesep)

def sub_keyword(mo):
    '''re.subs() argument to tag keywords.'''
    word=mo.group('word');
    stag,etag = keywordtags[backend]
    return stag+word+etag

def code_filter():
    '''This function does all the work.'''
    global language, backend
    line = sys.stdin.readline()
    while line:
        line = string.rstrip(line)
        # Escape special characters.
        line = string.replace(line,'&','&amp;')
        line = string.replace(line,'<','&lt;')
        line = string.replace(line,'>','&gt;')
        #line = re.sub(r'[—-]\s*(?P<word>\s*[0-9IVXP]+)\s*[—-]',sub_keyword,line)
        line = re.sub(r'pagenumber::original\[(?P<word>[0-9IVXP]+)]',sub_keyword,line)
        sys.stdout.write(line + os.linesep)
        line = sys.stdin.readline()

def usage(msg=''):
    if msg:
        print_stderr(msg)
    print_stderr('Usage: orig_pagenumber -b backend')
    print_stderr('                   [ --help | -h ] [ --version | -v ]')

def main():
    global language, backend, tabsize
    # Process command line options.
    import getopt
    opts,args = getopt.getopt(sys.argv[1:],
        'b:l:ht:v',
        ['help','version'])
    if len(args) > 0:
        usage()
        sys.exit(1)
    for o,v in opts:
        if o in ('--help','-h'):
            print __doc__
            sys.exit(0)
        if o in ('--version','-v'):
            print('orig_pagenumber version %s' % (VERSION,))
            sys.exit(0)
        if o == '-b': backend = v
    if backend is None:
        usage('backend option is mandatory')
        sys.exit(1)
    if not keywordtags.has_key(backend):
        usage('illegal backend option')
        sys.exit(1)
    # Do the work.
    code_filter()

if __name__ == "__main__":
    try:
        main()
        sys.exit(0)
    except (KeyboardInterrupt, SystemExit):
        pass
    except:
        print_stderr("%s: unexpected exit status: %s" %
            (os.path.basename(sys.argv[0]), sys.exc_info()[1]))
    # Exit with previous sys.exit() status or zero if no sys.exit().
    sys.exit(sys.exc_info()[1])

Reply via email to