Pārlūkot izejas kodu

Make Template.remove(keep_field=True) slightly more reasonable.

tags/v0.5
Ben Kurtovic pirms 7 gadiem
vecāks
revīzija
6159171e04
4 mainītis faili ar 31 papildinājumiem un 15 dzēšanām
  1. +3
    -1
      CHANGELOG
  2. +4
    -1
      docs/changelog.rst
  3. +21
    -13
      mwparserfromhell/nodes/template.py
  4. +3
    -0
      tests/test_template.py

+ 3
- 1
CHANGELOG Parādīt failu

@@ -1,6 +1,8 @@
v0.5 (unreleased):

- Fixed Wikicode.matches() on iterables besides lists and tuples.
- Made Template.remove(keep_field=True) behave more reasonably when the
parameter is already empty.
- Fixed Wikicode.matches()'s behavior on iterables besides lists and tuples.
- Fixed len() sometimes raising ValueError on empty node lists.
- Fixed release script after changes to PyPI.



+ 4
- 1
docs/changelog.rst Parādīt failu

@@ -7,7 +7,10 @@ v0.5
Unreleased
(`changes <https://github.com/earwig/mwparserfromhell/compare/v0.4.4...develop>`__):

- Fixed :meth:`.Wikicode.matches` on iterables besides lists and tuples.
- Made :meth:`Template.remove(keep_field=True) <.Template.remove>` behave more
reasonably when the parameter is already empty.
- Fixed :meth:`.Wikicode.matches`\ 's behavior on iterables besides lists and
tuples.
- Fixed ``len()`` sometimes raising ``ValueError`` on empty node lists.
- Fixed release script after changes to PyPI.



+ 21
- 13
mwparserfromhell/nodes/template.py Parādīt failu

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
# Copyright (C) 2012-2017 Ben Kurtovic <ben.kurtovic@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -70,7 +70,8 @@ class Template(Node):
get(param.value)
write("}}")

def _surface_escape(self, code, char):
@staticmethod
def _surface_escape(code, char):
"""Return *code* with *char* escaped as an HTML entity.

The main use of this is to escape pipes (``|``) or equal signs (``=``)
@@ -82,7 +83,8 @@ class Template(Node):
if char in node:
code.replace(node, node.replace(char, replacement), False)

def _select_theory(self, theories):
@staticmethod
def _select_theory(theories):
"""Return the most likely spacing convention given different options.

Given a dictionary of convention options as keys and their occurrence
@@ -96,6 +98,22 @@ class Template(Node):
if confidence >= 0.75:
return tuple(theories.keys())[values.index(best)]

@staticmethod
def _blank_param_value(value):
"""Remove the content from *value* while keeping its whitespace.

Replace *value*\ 's nodes with two text nodes, the first containing
whitespace from before its content and the second containing whitespace
from after its content.
"""
sval = str(value)
if sval.isspace():
before, after = "", sval
else:
match = re.search(r"^(\s*).*?(\s*)$", sval, FLAGS)
before, after = match.group(1), match.group(2)
value.nodes = [Text(before), Text(after)]

def _get_spacing_conventions(self, use_names):
"""Try to determine the whitespace conventions for parameters.

@@ -119,16 +137,6 @@ class Template(Node):
after = self._select_theory(after_theories)
return before, after

def _blank_param_value(self, value):
"""Remove the content from *value* while keeping its whitespace.

Replace *value*\ 's nodes with two text nodes, the first containing
whitespace from before its content and the second containing whitespace
from after its content.
"""
match = re.search(r"^(\s*).*?(\s*)$", str(value), FLAGS)
value.nodes = [Text(match.group(1)), Text(match.group(2))]

def _fix_dependendent_params(self, i):
"""Unhide keys if necessary after removing the param at index *i*."""
if not self.params[i].showkey:


+ 3
- 0
tests/test_template.py Parādīt failu

@@ -216,6 +216,7 @@ class TestTemplate(TreeEqualityTestCase):
node39 = Template(wraptext("a"), [pgenh("1", " b ")])
node40 = Template(wraptext("a"), [pgenh("1", " b"), pgenh("2", " c")])
node41 = Template(wraptext("a"), [pgens("1", " b"), pgens("2", " c")])
node42 = Template(wraptext("a"), [pgens("b", " \n")])

node1.add("e", "f", showkey=True)
node2.add(2, "g", showkey=False)
@@ -261,6 +262,7 @@ class TestTemplate(TreeEqualityTestCase):
node39.add("1", "c")
node40.add("3", "d")
node41.add("3", "d")
node42.add("b", "hello")

self.assertEqual("{{a|b=c|d|e=f}}", node1)
self.assertEqual("{{a|b=c|d|g}}", node2)
@@ -308,6 +310,7 @@ class TestTemplate(TreeEqualityTestCase):
self.assertEqual("{{a|c}}", node39)
self.assertEqual("{{a| b| c|d}}", node40)
self.assertEqual("{{a|1= b|2= c|3= d}}", node41)
self.assertEqual("{{a|b=hello \n}}", node42)

def test_remove(self):
"""test Template.remove()"""


Notiek ielāde…
Atcelt
Saglabāt