| 1 |
# Copyright (c) 2019 Shaun McCance <[email protected]> |
| 2 |
# |
| 3 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 |
# of this software and associated documentation files (the "Software"), to deal |
| 5 |
# in the Software without restriction, including without limitation the rights |
| 6 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 |
# copies of the Software, and to permit persons to whom the Software is |
| 8 |
# furnished to do so, subject to the following conditions: |
| 9 |
# |
| 10 |
# The above copyright notice and this permission notice shall be included in |
| 11 |
# all copies or substantial portions of the Software. |
| 12 |
# |
| 13 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 |
# THE SOFTWARE. |
| 20 |
|
| 21 |
import mallard.ducktype |
| 22 |
|
| 23 |
NSDB = 'http://docbook.org/ns/docbook' |
| 24 |
|
| 25 |
# FIXME: change output file extension |
| 26 |
|
| 27 |
leafs = ('abbrev', 'acronym', 'address', 'arg', 'artpagenums', 'authorinitials', |
| 28 |
'bibliocoverage', 'biblioid', 'bibliomisc', 'bibliomixed', 'bibliomset', |
| 29 |
'bibliorelation', 'bibliosource', 'bridgehead', 'caption', 'citation', |
| 30 |
'classsynopsisinfo', 'command', 'confdates', 'confnum', 'confsponsor', |
| 31 |
'conftitle', 'contractnum', 'contractsponsor', 'contrib', 'date', |
| 32 |
'edition', 'email', 'exceptionname', 'firstname', 'funcdef', 'funcparams', |
| 33 |
'funcsynopsisinfo', 'givenname', 'glosssee', 'glossseealso', 'glossterm', |
| 34 |
'holder', 'honorific', 'initializer', 'issuenum', 'jobtitle', 'keyword', |
| 35 |
'lhs', 'lineage', 'link', 'literallayout', 'manvolnum', 'member', |
| 36 |
'methodname', 'modifier', 'msgaud', 'msglevel', 'msgorig', 'option', |
| 37 |
'orgdiv', 'orgname', 'othername', 'pagenums', 'para', 'paramdef', |
| 38 |
'parameter', 'personname', 'phrase', 'primary', 'primaryie', 'productname', |
| 39 |
'productnumber', 'programlisting', 'pubdate', 'publishername', 'quote', |
| 40 |
'refclass', 'refdescriptor', 'refentrytitle', 'refmiscinfo', 'refname', |
| 41 |
'refpurpose', 'releaseinfo', 'remark', 'replaceable', 'revnumber', |
| 42 |
'revremark', 'rhs', 'screen', 'secondary', 'secondaryie', 'see', |
| 43 |
'seealso', 'seealsoie', 'seeie', 'seg', 'segtitle', 'seriesvolnums', |
| 44 |
'shortaffil', 'simpara', 'subjectterm', 'subtitle', 'surname', |
| 45 |
'synopfragmentref', 'synopsis', 'term', 'tertiary', 'tertiaryie', |
| 46 |
'title', 'titleabbrev', 'tocentry', 'type', 'uri', 'varname', |
| 47 |
'volumenum', 'year') |
| 48 |
|
| 49 |
class DocBookNodeFactory(mallard.ducktype.parser.NodeFactory): |
| 50 |
def __init__(self, parser): |
| 51 |
super().__init__(parser) |
| 52 |
self.id_attribute = 'xml:id' |
| 53 |
|
| 54 |
def create_block_node(self, name, outer): |
| 55 |
node = mallard.ducktype.parser.Block(name, outer=outer, parser=self.parser) |
| 56 |
if name in leafs: |
| 57 |
node.is_leaf = True |
| 58 |
return node |
| 59 |
|
| 60 |
def create_block_paragraph_node(self, outer): |
| 61 |
return self.create_block_node('para', outer=outer) |
| 62 |
|
| 63 |
def create_info_node(self, name, outer): |
| 64 |
node = mallard.ducktype.parser.Info(name, outer=outer, parser=self.parser) |
| 65 |
if name in leafs: |
| 66 |
node.is_leaf = True |
| 67 |
return node |
| 68 |
|
| 69 |
def create_info_paragraph_node(self, outer): |
| 70 |
return self.create_info_node('para', outer=outer) |
| 71 |
|
| 72 |
def handle_division_title(self, depth, inner): |
| 73 |
name = 'article' if (depth == 1) else 'section' |
| 74 |
page = mallard.ducktype.parser.Division(name, depth=depth, parser=self.parser) |
| 75 |
title = mallard.ducktype.parser.Block('title', inner=inner, parser=self.parser) |
| 76 |
self.parser.current.add_child(page) |
| 77 |
page.add_child(title) |
| 78 |
self.parser.current = title |
| 79 |
|
| 80 |
def handle_info_container(self, outer): |
| 81 |
info = mallard.ducktype.parser.Block('info', outer=outer, parser=self.parser) |
| 82 |
self.parser.current.add_child(info) |
| 83 |
self.parser.current.info = info |
| 84 |
info.parent = self.parser.current |
| 85 |
|
| 86 |
def handle_block_item_content(self, outer, inner): |
| 87 |
# bibliolist |
| 88 |
# calloutlist/callout |
| 89 |
# glosslist |
| 90 |
# FIXME: procedure/step substeps/step |
| 91 |
# qandaset |
| 92 |
# segmentedlist |
| 93 |
# simplelist |
| 94 |
# tocentry |
| 95 |
# variablelist |
| 96 |
if self.parser.current.is_name(('itemizedlist', 'orderedlist')): |
| 97 |
item = mallard.ducktype.parser.Block('listitem', outer=outer, inner=inner, parser=self.parser) |
| 98 |
self.parser.current.add_child(item) |
| 99 |
self.parser.current = item |
| 100 |
return item |
| 101 |
else: |
| 102 |
node = mallard.ducktype.parser.Block('itemizedlist', outer=outer, parser=self.parser) |
| 103 |
self.parser.current.add_child(node) |
| 104 |
item = mallard.ducktype.parser.Block('listitem', outer=outer, inner=inner, parser=self.parser) |
| 105 |
node.add_child(item) |
| 106 |
self.parser.current = item |
| 107 |
return item |
| 108 |
|
| 109 |
class DocBookExtension(mallard.ducktype.parser.ParserExtension): |
| 110 |
def __init__(self, parser, prefix, version): |
| 111 |
if version == 'experimental': |
| 112 |
self.version = version |
| 113 |
else: |
| 114 |
raise mallard.ducktype.parser.SyntaxError( |
| 115 |
'Unsupported docbook extension version: ' + version, |
| 116 |
parser) |
| 117 |
self.parser = parser |
| 118 |
self.prefix = prefix |
| 119 |
self.version = version |
| 120 |
self.parser.document.default_namespace = NSDB |
| 121 |
|
| 122 |
parser.factory = DocBookNodeFactory(parser) |