1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/serialisers/pretty/moin.py Fri Jun 30 00:40:39 2023 +0200
1.3 @@ -0,0 +1,168 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Prettyprinted text serialiser for Moin nodes.
1.8 +
1.9 +Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from moinformat.serialisers.pretty.common import Serialiser
1.26 +
1.27 +class MoinSerialiser(Serialiser):
1.28 +
1.29 + "Serialisation of Moin nodes for inspection."
1.30 +
1.31 + input_formats = ["moin", "wiki"]
1.32 + formats = ["pretty"]
1.33 +
1.34 + # Node handler methods.
1.35 +
1.36 + def region(self, region):
1.37 + self.out("%sRegion: level=%d indent=%d type=%s args=%r extra=%r\n" % (
1.38 + self.output.indent, region.level, region.indent, region.type, region.args,
1.39 + region.extra))
1.40 + self.visit_region(region)
1.41 +
1.42 + # Block node methods.
1.43 +
1.44 + def block(self, block):
1.45 + self.out("%sBlock\n" % self.output.indent)
1.46 + self.container(block)
1.47 +
1.48 + def defitem(self, defitem):
1.49 + self.out("%sDefItem: pad=%r extra=%r\n" % (self.output.indent,
1.50 + defitem.pad, defitem.extra))
1.51 + self.container(defitem)
1.52 +
1.53 + def defterm(self, defterm):
1.54 + self.out("%sDefTerm: pad=%r extra=%r\n" % (self.output.indent,
1.55 + defterm.pad, defterm.extra))
1.56 + self.container(defterm)
1.57 +
1.58 + def fontstyle(self, fontstyle):
1.59 + self.out("%sFontStyle: emphasis=%r strong=%r\n" % (self.output.indent,
1.60 + fontstyle.emphasis, fontstyle.strong))
1.61 + self.container(fontstyle)
1.62 +
1.63 + def heading(self, heading):
1.64 + self.out("%sHeading: level=%d start_extra=%r start_pad=%r end_pad=%r"
1.65 + " end_extra=%r identifier=%r\n" % (
1.66 + self.output.indent, heading.level, heading.start_extra,
1.67 + heading.start_pad, heading.end_pad, heading.end_extra,
1.68 + heading.identifier))
1.69 + self.container(heading)
1.70 +
1.71 + def link_label(self, link_label):
1.72 + self.out("%sLinkLabel\n" % self.output.indent)
1.73 + self.container(link_label)
1.74 +
1.75 + def link_parameter(self, link_parameter):
1.76 + self.out("%sLinkParameter\n" % self.output.indent)
1.77 + self.container(link_parameter)
1.78 +
1.79 + def list(self, list):
1.80 + self.out("%sList: indent=%r marker=%r num=%r\n" % (
1.81 + self.output.indent, list.indent, list.marker, list.num))
1.82 + self.container(list)
1.83 +
1.84 + def listitem(self, listitem):
1.85 + self.out("%sListItem: indent=%d marker=%r space=%r num=%r\n" % (
1.86 + self.output.indent, listitem.indent, listitem.marker, listitem.space, listitem.num))
1.87 + self.container(listitem)
1.88 +
1.89 + def table(self, table):
1.90 + self.out("%sTable:\n" % self.output.indent)
1.91 + self.container(table)
1.92 +
1.93 + def table_attrs(self, table_attrs):
1.94 + self.out("%sTableAttrs:\n" % self.output.indent)
1.95 + self.container(table_attrs)
1.96 +
1.97 + def table_cell(self, table_cell):
1.98 + self.out("%sTableCell: leading=%r padding=%r\n" % (
1.99 + self.output.indent, table_cell.leading, table_cell.padding))
1.100 + self.container(table_cell)
1.101 +
1.102 + def table_row(self, table_row):
1.103 + self.out("%sTableRow: trailing=%r leading=%r padding=%r\n" % (
1.104 + self.output.indent, table_row.trailing, table_row.leading,
1.105 + table_row.padding))
1.106 + self.container(table_row)
1.107 +
1.108 + def inline(self, inline):
1.109 + self.out("%s%s\n" % (self.output.indent, inline.__class__.__name__))
1.110 +
1.111 + # Inline nodes with children.
1.112 +
1.113 + def inline_container(self, inline):
1.114 + self.inline(inline)
1.115 + self.container(inline)
1.116 +
1.117 + larger = inline_container
1.118 +
1.119 + def link(self, link):
1.120 + self.out("%sLink: target=%r\n" % (self.output.indent, link.target))
1.121 + self.container(link)
1.122 +
1.123 + def macro(self, macro):
1.124 + self.out("%sMacro: name=%r args=%r\n" % (self.output.indent, macro.name, macro.args))
1.125 + self.container(macro)
1.126 +
1.127 + monospace = inline_container
1.128 + smaller = inline_container
1.129 + strikethrough = inline_container
1.130 + subscript = inline_container
1.131 + superscript = inline_container
1.132 +
1.133 + def transclusion(self, transclusion):
1.134 + self.out("%sTransclusion: target=%r\n" % (self.output.indent, transclusion.target))
1.135 + self.container(transclusion)
1.136 +
1.137 + underline = inline_container
1.138 +
1.139 + # Inline nodes without children.
1.140 +
1.141 + def anchor(self, anchor):
1.142 + self.out("%sAnchor: target=%r\n" % (self.output.indent, anchor.target))
1.143 +
1.144 + break_ = inline
1.145 +
1.146 + def comment(self, comment):
1.147 + self.out("%sComment: comment=%r extra=%r\n" % (self.output.indent, comment.comment, comment.extra))
1.148 +
1.149 + def directive(self, directive):
1.150 + self.out("%sDirective: directive=%r extra=%r\n" % (self.output.indent, directive.directive, directive.extra))
1.151 +
1.152 + linebreak = inline
1.153 + nbsp = inline
1.154 +
1.155 + def rule(self, rule):
1.156 + self.out("%sRule: height=%d\n" % (self.output.indent, rule.height))
1.157 +
1.158 + def table_attr(self, table_attr):
1.159 + self.out("%sTableAttr: name=%r value=%r concise=%r quote=%r\n" % (
1.160 + self.output.indent, table_attr.name, table_attr.value,
1.161 + table_attr.concise, table_attr.quote))
1.162 +
1.163 + def text(self, text):
1.164 + self.out("%sText: %r\n" % (self.output.indent, text.s))
1.165 +
1.166 + def verbatim(self, verbatim):
1.167 + self.out("%sVerbatim: text=%r\n" % (self.output.indent, verbatim.text))
1.168 +
1.169 +serialiser = MoinSerialiser
1.170 +
1.171 +# vim: tabstop=4 expandtab shiftwidth=4