paul@333 | 1 | #!/usr/bin/env python |
paul@333 | 2 | |
paul@333 | 3 | """ |
paul@333 | 4 | Prettyprinted text serialiser for Moin nodes. |
paul@333 | 5 | |
paul@333 | 6 | Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> |
paul@333 | 7 | |
paul@333 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@333 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@333 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@333 | 11 | version. |
paul@333 | 12 | |
paul@333 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@333 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@333 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@333 | 16 | details. |
paul@333 | 17 | |
paul@333 | 18 | You should have received a copy of the GNU General Public License along with |
paul@333 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@333 | 20 | """ |
paul@333 | 21 | |
paul@333 | 22 | from moinformat.serialisers.pretty.common import Serialiser |
paul@333 | 23 | |
paul@333 | 24 | class MoinSerialiser(Serialiser): |
paul@333 | 25 | |
paul@333 | 26 | "Serialisation of Moin nodes for inspection." |
paul@333 | 27 | |
paul@333 | 28 | input_formats = ["moin", "wiki"] |
paul@333 | 29 | formats = ["pretty"] |
paul@333 | 30 | |
paul@333 | 31 | # Node handler methods. |
paul@333 | 32 | |
paul@333 | 33 | def region(self, region): |
paul@333 | 34 | self.out("%sRegion: level=%d indent=%d type=%s args=%r extra=%r\n" % ( |
paul@333 | 35 | self.output.indent, region.level, region.indent, region.type, region.args, |
paul@333 | 36 | region.extra)) |
paul@333 | 37 | self.visit_region(region) |
paul@333 | 38 | |
paul@333 | 39 | # Block node methods. |
paul@333 | 40 | |
paul@333 | 41 | def block(self, block): |
paul@333 | 42 | self.out("%sBlock\n" % self.output.indent) |
paul@333 | 43 | self.container(block) |
paul@333 | 44 | |
paul@333 | 45 | def defitem(self, defitem): |
paul@333 | 46 | self.out("%sDefItem: pad=%r extra=%r\n" % (self.output.indent, |
paul@333 | 47 | defitem.pad, defitem.extra)) |
paul@333 | 48 | self.container(defitem) |
paul@333 | 49 | |
paul@333 | 50 | def defterm(self, defterm): |
paul@333 | 51 | self.out("%sDefTerm: pad=%r extra=%r\n" % (self.output.indent, |
paul@333 | 52 | defterm.pad, defterm.extra)) |
paul@333 | 53 | self.container(defterm) |
paul@333 | 54 | |
paul@333 | 55 | def fontstyle(self, fontstyle): |
paul@333 | 56 | self.out("%sFontStyle: emphasis=%r strong=%r\n" % (self.output.indent, |
paul@333 | 57 | fontstyle.emphasis, fontstyle.strong)) |
paul@333 | 58 | self.container(fontstyle) |
paul@333 | 59 | |
paul@333 | 60 | def heading(self, heading): |
paul@333 | 61 | self.out("%sHeading: level=%d start_extra=%r start_pad=%r end_pad=%r" |
paul@333 | 62 | " end_extra=%r identifier=%r\n" % ( |
paul@333 | 63 | self.output.indent, heading.level, heading.start_extra, |
paul@333 | 64 | heading.start_pad, heading.end_pad, heading.end_extra, |
paul@333 | 65 | heading.identifier)) |
paul@333 | 66 | self.container(heading) |
paul@333 | 67 | |
paul@333 | 68 | def link_label(self, link_label): |
paul@333 | 69 | self.out("%sLinkLabel\n" % self.output.indent) |
paul@333 | 70 | self.container(link_label) |
paul@333 | 71 | |
paul@333 | 72 | def link_parameter(self, link_parameter): |
paul@333 | 73 | self.out("%sLinkParameter\n" % self.output.indent) |
paul@333 | 74 | self.container(link_parameter) |
paul@333 | 75 | |
paul@333 | 76 | def list(self, list): |
paul@333 | 77 | self.out("%sList: indent=%r marker=%r num=%r\n" % ( |
paul@333 | 78 | self.output.indent, list.indent, list.marker, list.num)) |
paul@333 | 79 | self.container(list) |
paul@333 | 80 | |
paul@333 | 81 | def listitem(self, listitem): |
paul@333 | 82 | self.out("%sListItem: indent=%d marker=%r space=%r num=%r\n" % ( |
paul@333 | 83 | self.output.indent, listitem.indent, listitem.marker, listitem.space, listitem.num)) |
paul@333 | 84 | self.container(listitem) |
paul@333 | 85 | |
paul@333 | 86 | def table(self, table): |
paul@333 | 87 | self.out("%sTable:\n" % self.output.indent) |
paul@333 | 88 | self.container(table) |
paul@333 | 89 | |
paul@333 | 90 | def table_attrs(self, table_attrs): |
paul@333 | 91 | self.out("%sTableAttrs:\n" % self.output.indent) |
paul@333 | 92 | self.container(table_attrs) |
paul@333 | 93 | |
paul@333 | 94 | def table_cell(self, table_cell): |
paul@333 | 95 | self.out("%sTableCell: leading=%r padding=%r\n" % ( |
paul@333 | 96 | self.output.indent, table_cell.leading, table_cell.padding)) |
paul@333 | 97 | self.container(table_cell) |
paul@333 | 98 | |
paul@333 | 99 | def table_row(self, table_row): |
paul@333 | 100 | self.out("%sTableRow: trailing=%r leading=%r padding=%r\n" % ( |
paul@333 | 101 | self.output.indent, table_row.trailing, table_row.leading, |
paul@333 | 102 | table_row.padding)) |
paul@333 | 103 | self.container(table_row) |
paul@333 | 104 | |
paul@333 | 105 | def inline(self, inline): |
paul@333 | 106 | self.out("%s%s\n" % (self.output.indent, inline.__class__.__name__)) |
paul@333 | 107 | |
paul@333 | 108 | # Inline nodes with children. |
paul@333 | 109 | |
paul@333 | 110 | def inline_container(self, inline): |
paul@333 | 111 | self.inline(inline) |
paul@333 | 112 | self.container(inline) |
paul@333 | 113 | |
paul@333 | 114 | larger = inline_container |
paul@333 | 115 | |
paul@333 | 116 | def link(self, link): |
paul@333 | 117 | self.out("%sLink: target=%r\n" % (self.output.indent, link.target)) |
paul@333 | 118 | self.container(link) |
paul@333 | 119 | |
paul@333 | 120 | def macro(self, macro): |
paul@333 | 121 | self.out("%sMacro: name=%r args=%r\n" % (self.output.indent, macro.name, macro.args)) |
paul@333 | 122 | self.container(macro) |
paul@333 | 123 | |
paul@333 | 124 | monospace = inline_container |
paul@333 | 125 | smaller = inline_container |
paul@333 | 126 | strikethrough = inline_container |
paul@333 | 127 | subscript = inline_container |
paul@333 | 128 | superscript = inline_container |
paul@333 | 129 | |
paul@333 | 130 | def transclusion(self, transclusion): |
paul@333 | 131 | self.out("%sTransclusion: target=%r\n" % (self.output.indent, transclusion.target)) |
paul@333 | 132 | self.container(transclusion) |
paul@333 | 133 | |
paul@333 | 134 | underline = inline_container |
paul@333 | 135 | |
paul@333 | 136 | # Inline nodes without children. |
paul@333 | 137 | |
paul@333 | 138 | def anchor(self, anchor): |
paul@333 | 139 | self.out("%sAnchor: target=%r\n" % (self.output.indent, anchor.target)) |
paul@333 | 140 | |
paul@333 | 141 | break_ = inline |
paul@333 | 142 | |
paul@333 | 143 | def comment(self, comment): |
paul@333 | 144 | self.out("%sComment: comment=%r extra=%r\n" % (self.output.indent, comment.comment, comment.extra)) |
paul@333 | 145 | |
paul@333 | 146 | def directive(self, directive): |
paul@333 | 147 | self.out("%sDirective: directive=%r extra=%r\n" % (self.output.indent, directive.directive, directive.extra)) |
paul@333 | 148 | |
paul@333 | 149 | linebreak = inline |
paul@333 | 150 | nbsp = inline |
paul@333 | 151 | |
paul@333 | 152 | def rule(self, rule): |
paul@333 | 153 | self.out("%sRule: height=%d\n" % (self.output.indent, rule.height)) |
paul@333 | 154 | |
paul@333 | 155 | def table_attr(self, table_attr): |
paul@333 | 156 | self.out("%sTableAttr: name=%r value=%r concise=%r quote=%r\n" % ( |
paul@333 | 157 | self.output.indent, table_attr.name, table_attr.value, |
paul@333 | 158 | table_attr.concise, table_attr.quote)) |
paul@333 | 159 | |
paul@333 | 160 | def text(self, text): |
paul@333 | 161 | self.out("%sText: %r\n" % (self.output.indent, text.s)) |
paul@333 | 162 | |
paul@333 | 163 | def verbatim(self, verbatim): |
paul@333 | 164 | self.out("%sVerbatim: text=%r\n" % (self.output.indent, verbatim.text)) |
paul@333 | 165 | |
paul@333 | 166 | serialiser = MoinSerialiser |
paul@333 | 167 | |
paul@333 | 168 | # vim: tabstop=4 expandtab shiftwidth=4 |