summaryrefslogtreecommitdiff
path: root/a2x
blob: 7bcf1266957b85aad787bf90112120925d87e3f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#!/usr/bin/env bash
#
# a2x - convert Asciidoc text file to PDF, XHTML, HTML Help, manpage
#                  or plain text
#
# Copyright (C) 2007 Stuart Rackham. Free use of this software is granted
# under the terms of the GNU General Public License (GPL).
#

VERSION=1.0.0
BASENAME=$(basename "$0")
REALNAME="$0"
if [ ! -e "$REALNAME" ]; then
    REALNAME=$(which "$REALNAME")
fi
REALNAME="$(readlink -f "$REALNAME")"
CONF_DIR=/etc/asciidoc
# FOP executable is named fop on some systems.
FOP_COMMAND="fop.sh"
if ! which "$FOP_COMMAND" >/dev/null 2>&1; then
    FOP_COMMAND="fop"
fi

#--------------------------------------------------------------------
# Constants.
#--------------------------------------------------------------------

# These are mostly related to command options and are set by parse_options().
ASCIIDOC_OPTS=
COPY=no
DESTINATION_DIR=
DOCTYPE=
DRY_RUN=no
FORMAT=xhtml
ICONS=no
ICONS_DIR=./images/icons
SKIP_ASCIIDOC=no
SRC_DIR=
SRC_FILE=
SRC_NAME=           # Source file name sans path and file name extension.
STYLESHEET=./docbook-xsl.css
VERBOSE_2=no
VERBOSE=no
XSLTPROC_OPTS=
DBLATEX_OPTS=
FOP_OPTS=
FOP=no
XMLLINT=yes

#--------------------------------------------------------------------
# General purpose functions
#--------------------------------------------------------------------

# Write $1 to stderr with backslash-escaped characters and no trailing newline.
function write_console()
{
        echo -ne "$1" >&2
}

# Write newline to stderr.
function newline()
{
        echo >&2
}

# Write $1 message to stderr.
function console_msg()
{
        echo "$BASENAME: $1" >&2
}

# Write $1 to stderr if verbose or dry-run options set.
function verbose_msg()
{
    if isyes "$VERBOSE" || isyes "$DRY_RUN"; then
            console_msg "$1"
    fi
}

# Return 0 if $1 is interpreted as an affirmative string.
function isyes()
{
    case "$1" in
        y|Y|yes|YES|Yes|true|TRUE|True) return 0;;
    esac
    return 1
}

# Log message $1 and exit with status $2 (default 1).
function quit()
{
    local err tmp
    err=${2:-1}
    if [ $err -ne 0 ]; then
        tmp="$VERBOSE"
        VERBOSE=yes     # Force console error exit message.
        console_msg "failed: $1"
        VERBOSE="$tmp"
    else
        console_msg "$1"
    fi
    cleanup
        exit $err
}

# Execute the $1 command in the current shell subject to VERBOSE, DRY_RUN shell
# variables.
function execute_command()
{
    if isyes "$VERBOSE" || isyes "$DRY_RUN"; then
    console_msg "eval: $1"
    fi
    if isyes "$DRY_RUN"; then
        return 0
    else
        eval $1
        return $?
    fi
}

# Same as execute_command() but if error occurs prints optional $2 message and
# exits program.
function execute_command_2()
{
    local msg
    execute_command "$1"
    if [ $? -ne 0 ]; then
        if [ -n "$2" ]; then
            msg="$2"
        else
            msg="$1"
        fi
        quit "$msg"
    fi
}

# Return 0 if $1 command can be executed.
function is_executable()
{
    if which "$1" >/dev/null 2>&1; then
        return 0
    else
        return 1
    fi
}

# Return 127 if $1 is not in search path else return 0.
function require()
{
    if ! is_executable "$1"; then
        quit "cannot find required program: $1" 127
    fi
}

# Join path $1 to path $2.
function join()
{
    if [ -n "$1" ]; then
        echo "$1/$2"
    else
        echo "$2"
    fi
}

# Echo the total size in bytes of file name arguments.
function file_size()
{
    echo $(du -cb "$@" | tail -1 | awk '{print $1}')
}

#--------------------------------------------------------------------
# Application specific functions
#--------------------------------------------------------------------

# Trap interrupts.
function set_trap()
{
    # By convention exit code is 128 + signal number.
    trap "newline; quit 'exiting: SIGINT' 130" SIGINT
    trap "newline; quit 'exiting: SIGQUIT' 131" SIGQUIT
    trap "quit 'exiting: SIGHUP' 129" SIGHUP
    trap "quit 'exiting: SIGTERM' 143" SIGTERM
}

# Called at program exit.
function cleanup()
{
    if [ "$(pwd)" != "$PWD" ]; then
        execute_command "cd \"$PWD\""
    fi
}

# Print help summary.
function help()
{
    cat <<EOF
synopsis:

  $BASENAME [OPTIONS] FILE

options:
  -a, --attribute=ATTRIBUTE      set asciidoc attribute value
  --asciidoc-opts=ASCIIDOC_OPTS  asciidoc options
  --copy                         copy icons or HTML stylesheet
  -D, --destination-dir=PATH     output directory (defaults to FILE directory)
  -d, --doctype=DOCTYPE          article, manpage, book
  -f, --format=FORMAT            chunked,htmlhelp,manpage,pdf,text,xhtml,dvi,ps,tex
  -h, --help                     print command syntax summary
  --icons                        use admonition, callout and navigation icons
  --icons-dir=PATH               admonition and navigation icon directory
  -L, --no-xmllint               don't check asciidoc output with xmllint(1)
  -n, --dry-run                  don't do anything just print the commands
  -s, --skip-asciidoc            skip asciidoc execution
  --stylesheet=PATH              target HTML CSS stylesheet file name
  --unsafe                       use asciidoc --unsafe option
  --version                      print program version to stdout
  -v, --verbose                  print operational details to stderr
  --dblatex-opts=DBLATEX_OPTS    dblatex options
  --fop                          use FOP to generate PDF files
  --fop-opts=FOP_OPTS            options for FOP pdf generation
  --xsltproc-opts=XSLTPROC_OPTS  xsltproc options
EOF
}

# Print full path name of file $1 searching first in the directory containing
# the asciidoc executable and then in the global configuration directory.
function conf_file()
{
    local result dir
    # First look in same directory as asciidoc executable.
    dir="$(dirname "$REALNAME")"
    if [ ! -f "$dir/$1" -a -d $CONF_DIR ]; then
        dir=$CONF_DIR
    fi
    result="$dir/$1"
    echo $result
}

#--------------------------------------------------------------------
# Process command-line arguments $@
#--------------------------------------------------------------------
function parse_options()
{
    if [ -z "$*" ]; then
        help; exit 0
    fi
    require "getopt"
    getopt -T >/dev/null
    if [ $? -ne 4 ]; then
        quit "enhanced getopt(1) required"
    fi
    short_opts="a:d:D:f:hLnsv"
    long_opts="attribute:,asciidoc-opts:,destination-dir:,doctype:,help,icons-dir:,dry-run,format:,copy,icons,skip-asciidoc,stylesheet:,unsafe,version,verbose,xsltproc-opts:,dblatex-opts:,fop,fop-opts:,no-xmllint"
    args=$(getopt -o $short_opts -l $long_opts -n $BASENAME -- "$@" 2>/dev/null)
    if [ $? -ne 0 ]; then
        quit "invalid command options, run: a2x --help"
    fi
    eval set -- "$args"     # Set positional variables.
    while true ; do
        case "$1" in
            -a|--attribute)
                ASCIIDOC_OPTS="$ASCIIDOC_OPTS -a \"$2\""
                shift 2 ;;
            --asciidoc-opts)
                ASCIIDOC_OPTS="$ASCIIDOC_OPTS $2"
                shift 2 ;;
            --copy)
                COPY=yes;
                shift ;;
            -d|--doctype)
                DOCTYPE=$2
                shift 2 ;;
            -D|--destination-dir)
                DESTINATION_DIR=$2
                shift 2 ;;
            -f|--format)
                FORMAT=$2
                shift 2 ;;
            -h|--help)
                help; exit 0 ;;
            --icons)
                ICONS=yes
                shift ;;
            --icons-dir)
                ICONS_DIR=$2
                shift 2 ;;
            -n|--dry-run)
                DRY_RUN=yes;
                shift ;;
            -s|--skip-asciidoc)
                SKIP_ASCIIDOC=yes;
                shift ;;
            --stylesheet)
                STYLESHEET=$2
                shift 2 ;;
            --unsafe)
                ASCIIDOC_OPTS="$ASCIIDOC_OPTS --unsafe";
                shift ;;
            --version)
                echo "$BASENAME $VERSION" ; exit 0 ;;
            -v|--verbose)
                if isyes "$VERBOSE"; then
                    VERBOSE_2=yes
                else
                    VERBOSE=yes
                fi
                shift ;;
            -L|--no-xmllint)
                XMLLINT=no;
                shift ;;
            --xsltproc-opts)
                XSLTPROC_OPTS="$XSLTPROC_OPTS $2"
                shift 2 ;;
            --fop)
                FOP=yes;
                shift ;;
            --fop-opts)
                FOP_OPTS="$FOP_OPTS $2"
                FOP=yes
                shift 2 ;;
            --dblatex-opts)
                DBLATEX_OPTS="$DBLATEX_OPTS $2"
                shift 2 ;;
            --)
                shift; break ;;
            *)
                quit "unrecognized option: $1" ;;
        esac
    done

    if isyes "$DRY_RUN"; then
        VERBOSE=yes
    fi

    if [ $# -eq 0 ]; then
        quit "source file not specified"
    fi
    if [ $# -ne 1 ]; then
        quit "only one source file allowed"
    fi
    if [ ! -r "$1" ]; then
        quit "source file not found: $1"
    fi
    SRC_FILE=$1
    SRC_DIR=$(dirname "$1")
    SRC_NAME=$1
    SRC_NAME=${SRC_NAME##*/}    # Strip path.
    SRC_NAME=${SRC_NAME%.*}     # Strip extension.
    # Use FOP if there is no dblatex.
    if ! is_executable dblatex && is_executable "$FOP_COMMAND"; then
        FOP=yes
    fi
}

#--------------------------------------------------------------------
# Validate program options.
#--------------------------------------------------------------------
function validate_options()
{
    case "$FORMAT" in
        chunked|dvi|htmlhelp|manpage|odt|pdf|ps|tex|text|xhtml) ;;
        *) quit "illegal format: $FORMAT" ;;
    esac

    if [ -z "$DOCTYPE" ]; then
        if [ "$FORMAT" = "manpage" ]; then
            DOCTYPE=manpage
        else
            DOCTYPE=article
        fi
    fi
    case "$DOCTYPE" in
        article|book|manpage) ;;
        *) quit "illegal doctype: $DOCTYPE" ;;
    esac

    if [ -z "$ICONS_DIR" ]; then
        quit "icons directory not specified"
    fi
    if [[ "$ICONS_DIR" == /* ]]; then
        quit "icons directory must be relative: $ICONS_DIR"
    fi
    ICONS_DIR=${ICONS_DIR%*/}     # Strip trailing backslash.

    if [ ! -z "$DESTINATION_DIR" ]; then
        if [ ! -d "$DESTINATION_DIR" ]; then
            quit "destination directory not found: $DESTINATION_DIR"
        fi
    else
        DESTINATION_DIR="$SRC_DIR"
    fi

    if [ -z "$STYLESHEET" ]; then
        quit "stylesheet cannot be blank"
    fi
    if [[ "$STYLESHEET" == /* ]]; then
        quit "stylesheet path must be relative: $STYLESHEET"
    fi
}

# Conditionally copy distribution stylesheet and admonition and navigation
# icons to destination directory $1.
function copy_stylesheet_and_icons()
{
    if isyes $COPY; then
        copy_stylesheet "$1"
        if isyes $ICONS; then
            copy_icons "$1/$ICONS_DIR"
        fi
    fi
}

# Copy distribution stylesheet to destination directory $1.
function copy_stylesheet()
{
    local src dst
    src=$(conf_file stylesheets/docbook-xsl.css)
    if [ ! -r "$src" ]; then
        quit "file not found: $src"
    fi
    dst="$1/$STYLESHEET"
    # Check we're not trying to copy the file onto itself.
    if [[ "$src" -ef "$dst" ]]; then
        return
    fi
    execute_command_2 "cp -u \"$src\" \"$dst\""
}

# Copy distribution admonition and navigation icons to destination directory
# $1.
function copy_icons()
{
    local src dst
    dst="$1"
    # Set source icons directory.
    src=$(conf_file images/icons/home.png)
    if [ ! -r "$src" ]; then
        quit "file not found: $src"
    fi
    src=$(dirname "$src")
    # Check we're not trying to copy the file onto itself.
    if [[ "$src" -ef "$dst" ]]; then
        return
    fi
    if [ -e "$dst" ]; then
        if [ ! -d "$dst" ]; then
            quit "icon destination must be a directory: $dst"
        fi
    else
        execute_command_2 "mkdir -p \"$dst\""
    fi
    execute_command_2 "cp -rfu  \"$src/\"* \"$dst\""
}

# Run xmllint(1) checking of $1 file if xmllint checking is enabled.
function execute_xmllint()
{
    if isyes "$XMLLINT"; then
        if ! is_executable "xmllint"; then
            console_msg "xmllint not found: skipping validity checking"
        else
            execute_command_2 "xmllint --nonet --noout --valid \"$1\""
        fi
    fi
}

#--------------------------------------------------------------------
# Format conversion functions.
#--------------------------------------------------------------------

# Convert AsciiDoc $SRC_FILE to DocBook XML if it is newer than the
# XML output file.  $1 has additional asciidoc(1) options.
function to_docbook()
{
    local xml
    xml="$SRC_DIR/$SRC_NAME.xml"
    if isyes $SKIP_ASCIIDOC; then
        if [ ! -r "$xml" ]; then
            quit "file not found: $xml"
        fi
        return
    fi
    require "asciidoc"
    execute_command_2 "asciidoc $ASCIIDOC_OPTS $1 -b docbook \"$SRC_FILE\""
    execute_xmllint "$xml"
}

function to_xhtml()
{
    require "xsltproc"
    local xsl xml html
    xsl=$(conf_file docbook-xsl/xhtml.xsl)
    if [ ! -r "$xsl" ]; then
        quit "file not found: $xsl"
    fi
    to_docbook
    xml=$(readlink -f "$SRC_DIR/$SRC_NAME.xml")
    html="$SRC_NAME.html"
    copy_stylesheet_and_icons "$DESTINATION_DIR"
    execute_command_2 "cd \"$DESTINATION_DIR\""
    execute_command_2 "xsltproc $XSLTPROC_OPTS --nonet \
        \"$xsl\" \"$xml\" >\"$html\""
    execute_command_2 "cd - >/dev/null"
}

function to_chunked()
{
    require "xsltproc"
    local chunkdir xsl xml hhp chm
    case "$FORMAT" in
        chunked)
            chunkdir="$DESTINATION_DIR/$SRC_NAME.chunked"
            xsl=chunked.xsl
        ;;
        htmlhelp)
            chunkdir="$DESTINATION_DIR/$SRC_NAME.htmlhelp"
            hhp="$SRC_NAME.hhp"
            chm="$SRC_NAME.chm"
            XSLTPROC_OPTS="$XSLTPROC_OPTS \
                --stringparam htmlhelp.hhp \"$hhp\"
                --stringparam htmlhelp.chm \"$chm\""
            xsl=htmlhelp.xsl
        ;;
    esac
    xsl=$(conf_file docbook-xsl/$xsl)
    if [ ! -r "$xsl" ]; then
        quit "file not found: $xsl"
    fi
    to_docbook
    xml=$(readlink -f "$SRC_DIR/$SRC_NAME.xml")
    if [ ! -d "$chunkdir" ]; then
        execute_command_2 "mkdir \"$chunkdir\""
    fi
    execute_command_2 "rm -f \"$chunkdir/*.html\""
    copy_stylesheet_and_icons "$chunkdir"
    execute_command_2 "cd \"$DESTINATION_DIR\""
    execute_command_2 "xsltproc $XSLTPROC_OPTS --nonet \
        --stringparam base.dir \"$(basename "$chunkdir")/\" \
        \"$xsl\" \"$xml\""
    execute_command_2 "cd - >/dev/null"
}

function to_manpage()
{
    require "xsltproc"
    local xsl xml
    xsl=$(conf_file docbook-xsl/manpage.xsl)
    if [ ! -r "$xsl" ]; then
        quit "file not found: $xsl"
    fi
    to_docbook "-d manpage"
    xml=$(readlink -f "$SRC_DIR/$SRC_NAME.xml")
    execute_command_2 "cd \"$DESTINATION_DIR\""
    execute_command_2 "xsltproc $XSLTPROC_OPTS --nonet \
        \"$xsl\" \"$xml\""
    execute_command_2 "cd - >/dev/null"
}

function run_dblatex()
{
    require "dblatex"
    local xsl xml sty
    to_docbook
    xml="$SRC_DIR/$SRC_NAME.xml"
    xsl=$(conf_file dblatex/asciidoc-dblatex.xsl)
    sty=$(conf_file dblatex/asciidoc-dblatex.sty)
    execute_command_2 "dblatex -t $FORMAT -p \"$xsl\" -s \"$sty\" $DBLATEX_OPTS \"$xml\""
}

function run_fop()
{
    local xsl xml fo pdf
    xml="$SRC_DIR/$SRC_NAME.xml"
    pdf="$DESTINATION_DIR/$SRC_NAME.pdf"
    require "xsltproc"
    require "$FOP_COMMAND"
    xsl=$(conf_file docbook-xsl/fo.xsl)
    if [ ! -r "$xsl" ]; then
        quit "file not found: $xsl"
    fi
    fo="$SRC_DIR/$SRC_NAME.fo"
    to_docbook
    execute_command_2 "xsltproc $XSLTPROC_OPTS --nonet \
        \"$xsl\" \"$xml\" >\"$fo\""
    execute_command_2 "\"$FOP_COMMAND\" $FOP_OPTS -fo \"$fo\" -pdf \"$pdf\""
}

function to_pdf()
{
    if isyes $FOP; then
        run_fop
    else
        run_dblatex
    fi
}

function to_odt()
{
    require "docbook2odf"
    local xml odt opts
    xml="$SRC_DIR/$SRC_NAME.xml"
    odt="$DESTINATION_DIR/$SRC_NAME.odt"
    opts="--force"
    if ! isyes $VERBOSE; then
        opts="$opts --quiet"
    fi
    to_docbook
    execute_command_2 "docbook2odf $opts \"$xml\" --output-file \"$odt\""
}

function to_text()
{
    require "asciidoc"
    local html text conf xsl
    html="$SRC_DIR/$SRC_NAME.html"
    text="$DESTINATION_DIR/$SRC_NAME.text"
    conf=$(conf_file text.conf)
    # Use lynx if there is no w3m.
    if ! is_executable w3m && is_executable lynx; then
        execute_command_2 "asciidoc $ASCIIDOC_OPTS -f "$conf" -b html4 \
            -o - \"$SRC_FILE\" | lynx -dump -stdin >\"$text\""
    else
        require "w3m"
        require "xsltproc"
        xsl=$(conf_file docbook-xsl/text.xsl)
        execute_command_2 "asciidoc $ASCIIDOC_OPTS -f \"$conf\" -b docbook \
            -o - \"$SRC_FILE\" \
            | xsltproc --nonet --xinclude \"$xsl\" - \
            | w3m -cols 70 -dump -T text/html -no-graph >\"$text\""

    fi
}

#--------------------------------------------------------------------
# Main
#--------------------------------------------------------------------

PWD=`pwd`
set_trap
parse_options "$@"
validate_options
ASCIIDOC_OPTS="--doctype=$DOCTYPE $ASCIIDOC_OPTS"
if isyes $VERBOSE_2; then
    ASCIIDOC_OPTS="$ASCIIDOC_OPTS --verbose"
    XSLTPROC_OPTS="$XSLTPROC_OPTS --verbose"
fi
case "$FORMAT" in
    xhtml|chunked|htmlhelp)
        XSLTPROC_OPTS="$XSLTPROC_OPTS \
            --stringparam html.stylesheet \"$STYLESHEET\""
    ;;
esac
if isyes $ICONS; then
    XSLTPROC_OPTS="$XSLTPROC_OPTS --stringparam callout.graphics 1 \
        --stringparam navig.graphics 0 \
        --stringparam admon.textlabel 0 \
        --stringparam admon.graphics 1 \
        --stringparam admon.graphics.path \"$ICONS_DIR/\" \
        --stringparam callout.graphics.path \"$ICONS_DIR/callouts/\" \
        --stringparam navig.graphics.path \"$ICONS_DIR/\""
else
    XSLTPROC_OPTS="$XSLTPROC_OPTS --stringparam callout.graphics 0 \
        --stringparam navig.graphics 0 \
        --stringparam admon.textlabel 1 \
        --stringparam admon.graphics 0"
fi
case "$FORMAT" in
    chunked|htmlhelp)   to_chunked;;
    manpage)            to_manpage;;
    odt)                to_odt;;
    pdf)                to_pdf;;
    text)               to_text;;
    xhtml)              to_xhtml;;
    dvi)                run_dblatex;;
    ps)                 run_dblatex;;
    tex)                run_dblatex;;
esac
cleanup

#--------------------------------------------------------------------
# vim: set et ts=4 sw=4 sts=4:
#--------------------------------------------------------------------