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