Selaa lähdekoodia

Handle integers as MediaWiki does.

tags/v0.1
Ben Kurtovic 12 vuotta sitten
vanhempi
commit
6fefc28cba
2 muutettua tiedostoa jossa 8 lisäystä ja 7 poistoa
  1. +2
    -4
      mwparserfromhell/nodes/template.py
  2. +6
    -3
      mwparserfromhell/parser/builder.py

+ 2
- 4
mwparserfromhell/nodes/template.py Näytä tiedosto

@@ -76,7 +76,7 @@ class Template(Node):
code.replace(node, node.replace(char, replacement)) code.replace(node, node.replace(char, replacement))


def _blank_param_value(self, value): def _blank_param_value(self, value):
match = re.search("^(\s*).*?(\s*)$", unicode(value), FLAGS)
match = re.search(r"^(\s*).*?(\s*)$", unicode(value), FLAGS)
value.nodes = [Text(match.group(1)), Text(match.group(2))] value.nodes = [Text(match.group(1)), Text(match.group(2))]


def _select_theory(self, theories): def _select_theory(self, theories):
@@ -149,10 +149,8 @@ class Template(Node):
else: else:
int_keys = set() int_keys = set()
for param in self.params: for param in self.params:
try:
if re.match(r"[1-9][0-9]*$", param.name.strip()):
int_keys.add(int(unicode(param.name))) int_keys.add(int(unicode(param.name)))
except ValueError:
pass
expected = min(set(range(1, len(int_keys) + 2)) - int_keys) expected = min(set(range(1, len(int_keys) + 2)) - int_keys)
if expected == int_name: if expected == int_name:
showkey = False showkey = False


+ 6
- 3
mwparserfromhell/parser/builder.py Näytä tiedosto

@@ -20,6 +20,8 @@
# 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.


import re

from . import tokens from . import tokens
from ..nodes import Heading, HTMLEntity, Tag, Template, Text from ..nodes import Heading, HTMLEntity, Tag, Template, Text
from ..nodes.extras import Attribute, Parameter from ..nodes.extras import Attribute, Parameter
@@ -76,11 +78,12 @@ class Builder(object):
name = self._pop() name = self._pop()
default = self._wrap(unicode(min(int_key_range - int_keys))) default = self._wrap(unicode(min(int_key_range - int_keys)))
param = self._handle_parameter(default) param = self._handle_parameter(default)
try:
if re.match(r"[1-9][0-9]*$", param.name.strip()):
# We try a more restrictive test for integers than
# try: int(), because "01" as a key will pass through int()
# correctly but is not a valid integer key in wikicode:
int_keys.add(int(unicode(param.name))) int_keys.add(int(unicode(param.name)))
int_key_range.add(len(int_keys) + 1) int_key_range.add(len(int_keys) + 1)
except ValueError:
pass
params.append(param) params.append(param)
elif isinstance(token, tokens.TemplateClose): elif isinstance(token, tokens.TemplateClose):
if not params: if not params:


Ladataan…
Peruuta
Tallenna