@@ -31,6 +31,10 @@ class Node(StringMixIn): | |||||
def __strip__(self, normalize, collapse): | def __strip__(self, normalize, collapse): | ||||
return None | return None | ||||
def __showtree__(self, write, get, mark): | |||||
write(unicode(self)) | |||||
from mwparserfromhell.nodes import extras | from mwparserfromhell.nodes import extras | ||||
from mwparserfromhell.nodes.text import Text | from mwparserfromhell.nodes.text import Text | ||||
from mwparserfromhell.nodes.heading import Heading | from mwparserfromhell.nodes.heading import Heading | ||||
@@ -40,6 +40,11 @@ class Heading(Node): | |||||
def __strip__(self, normalize, collapse): | def __strip__(self, normalize, collapse): | ||||
return self.title | return self.title | ||||
def __showtree__(self, write, get, mark): | |||||
write("=" * self.level) | |||||
get(self.title) | |||||
write("=" * self.level) | |||||
@property | @property | ||||
def title(self): | def title(self): | ||||
return self._title | return self._title | ||||
@@ -20,8 +20,7 @@ | |||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||
# SOFTWARE. | # SOFTWARE. | ||||
from mwparserfromhell.nodes import Node | |||||
from mwparserfromhell.nodes.extras import Attribute | |||||
from mwparserfromhell.nodes import Node, Text | |||||
__all__ = ["Tag"] | __all__ = ["Tag"] | ||||
@@ -86,7 +85,7 @@ class Tag(Node): | |||||
if not self.showtag: | if not self.showtag: | ||||
open_, close = self._translate() | open_, close = self._translate() | ||||
if self.self_closing: | if self.self_closing: | ||||
return _open | |||||
return open_ | |||||
else: | else: | ||||
return open_ + unicode(self.contents) + close | return open_ + unicode(self.contents) + close | ||||
@@ -104,7 +103,7 @@ class Tag(Node): | |||||
yield None, self | yield None, self | ||||
if self.showtag: | if self.showtag: | ||||
for child in getter(self.tag): | for child in getter(self.tag): | ||||
yield self.tag, tag | |||||
yield self.tag, child | |||||
for attr in self.attrs: | for attr in self.attrs: | ||||
for child in getter(attr.name): | for child in getter(attr.name): | ||||
yield attr.name, child | yield attr.name, child | ||||
@@ -119,7 +118,31 @@ class Tag(Node): | |||||
return self.contents.strip_code(normalize, collapse) | return self.contents.strip_code(normalize, collapse) | ||||
return None | return None | ||||
def translate(self): | |||||
def __showtree__(self, write, get, mark): | |||||
tagnodes = self.tag.nodes | |||||
if (not self.attrs and len(tagnodes) == 1 and | |||||
isinstance(tagnodes[0], Text)): | |||||
write("<" + unicode(tagnodes[0]) + ">") | |||||
else: | |||||
write("<") | |||||
get(self.tag) | |||||
for attr in self.attrs: | |||||
get(attr.name) | |||||
if not attr.value: | |||||
continue | |||||
write(" = ") | |||||
mark() | |||||
get(attr.value) | |||||
write(">") | |||||
get(self.contents) | |||||
if len(tagnodes) == 1 and isinstance(tagnodes[0], Text): | |||||
write("</" + unicode(tagnodes[0]) + ">") | |||||
else: | |||||
write("</") | |||||
get(self.tag) | |||||
write(">") | |||||
def _translate(self): | |||||
translations = { | translations = { | ||||
self.TAG_ITALIC: ("''", "''"), | self.TAG_ITALIC: ("''", "''"), | ||||
self.TAG_BOLD: ("'''", "'''"), | self.TAG_BOLD: ("'''", "'''"), | ||||
@@ -57,6 +57,18 @@ class Template(Node): | |||||
for child in getter(param.value): | for child in getter(param.value): | ||||
yield param.value, child | yield param.value, child | ||||
def __showtree__(self, write, get, mark): | |||||
write("{{") | |||||
get(self.name) | |||||
for param in self.params: | |||||
write(" | ") | |||||
mark() | |||||
get(param.name) | |||||
write(" = ") | |||||
mark() | |||||
get(param.value) | |||||
write("}}") | |||||
def _surface_escape(self, code, char): | def _surface_escape(self, code, char): | ||||
replacement = HTMLEntity(value=ord(char)) | replacement = HTMLEntity(value=ord(char)) | ||||
for node in code.filter_text(recursive=False): | for node in code.filter_text(recursive=False): | ||||
@@ -22,9 +22,7 @@ | |||||
import re | import re | ||||
from mwparserfromhell.nodes import ( | |||||
Heading, HTMLEntity, Node, Tag, Template, Text | |||||
) | |||||
from mwparserfromhell.nodes import Node, Template, Text | |||||
from mwparserfromhell.string_mixin import StringMixIn | from mwparserfromhell.string_mixin import StringMixIn | ||||
from mwparserfromhell.utils import parse_anything | from mwparserfromhell.utils import parse_anything | ||||
@@ -95,47 +93,10 @@ class Wikicode(StringMixIn): | |||||
else: | else: | ||||
lines.append(" " * 6 * indent + " ".join(args)) | lines.append(" " * 6 * indent + " ".join(args)) | ||||
get = lambda code: self._get_tree(code, lines, marker, indent + 1) | |||||
mark = lambda: lines.append(marker) | |||||
for node in code.nodes: | for node in code.nodes: | ||||
if isinstance(node, Heading): | |||||
write("=" * node.level) | |||||
self._get_tree(node.title, lines, marker, indent + 1) | |||||
write("=" * node.level) | |||||
elif isinstance(node, Tag): | |||||
tagnodes = node.tag.nodes | |||||
if (not node.attrs and len(tagnodes) == 1 and | |||||
isinstance(tagnodes[0], Text)): | |||||
write("<" + unicode(tagnodes[0]) + ">") | |||||
else: | |||||
write("<") | |||||
self._get_tree(node.tag, lines, marker, indent + 1) | |||||
for attr in node.attrs: | |||||
self._get_tree(attr.name, lines, marker, indent + 1) | |||||
if not attr.value: | |||||
continue | |||||
write(" = ") | |||||
lines.append(marker) # Continue from this line | |||||
self._get_tree(attr.value, lines, marker, indent + 1) | |||||
write(">") | |||||
self._get_tree(node.contents, lines, marker, indent + 1) | |||||
if len(tagnodes) == 1 and isinstance(tagnodes[0], Text): | |||||
write("</" + unicode(tagnodes[0]) + ">") | |||||
else: | |||||
write("</") | |||||
self._get_tree(node.tag, lines, marker, indent + 1) | |||||
write(">") | |||||
elif isinstance(node, Template): | |||||
write("{{") | |||||
self._get_tree(node.name, lines, marker, indent + 1) | |||||
for param in node.params: | |||||
write(" | ") | |||||
lines.append(marker) # Continue from this line | |||||
self._get_tree(param.name, lines, marker, indent + 1) | |||||
write(" = ") | |||||
lines.append(marker) # Continue from this line | |||||
self._get_tree(param.value, lines, marker, indent + 1) | |||||
write("}}") | |||||
else: | |||||
write(unicode(node)) | |||||
node.__showtree__(write, get, mark) | |||||
return lines | return lines | ||||
@property | @property | ||||