Selaa lähdekoodia

Updates for Template and others.

tags/v0.1
Ben Kurtovic 12 vuotta sitten
vanhempi
commit
1e314ee1bd
4 muutettua tiedostoa jossa 39 lisäystä ja 30 poistoa
  1. +3
    -1
      mwparserfromhell/nodes/html_entity.py
  2. +30
    -23
      mwparserfromhell/nodes/template.py
  3. +5
    -5
      mwparserfromhell/utils.py
  4. +1
    -1
      mwparserfromhell/wikicode.py

+ 3
- 1
mwparserfromhell/nodes/html_entity.py Näytä tiedosto

@@ -27,8 +27,10 @@ from mwparserfromhell.nodes import Node
__all__ = ["HTMLEntity"]

class HTMLEntity(Node):
def __init__(self, value, named, hexadecimal=False):
def __init__(self, value, named=None, hexadecimal=False):
self._value = value
if named is None: # Try to guess whether or not the entity is named
named = False if isinstance(value, int) else True
self._named = named
self._hexadecimal = hexadecimal



+ 30
- 23
mwparserfromhell/nodes/template.py Näytä tiedosto

@@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from mwparserfromhell.nodes import Node
from mwparserfromhell.nodes import HTMLEntity, Node
from mwparserfromhell.nodes.extras import Parameter
from mwparserfromhell.utils import parse_anything

@@ -41,8 +41,14 @@ class Template(Node):
else:
return "{{" + unicode(self.name) + "}}"

def _surface_escape(self, code, char):
replacement = HTMLEntity(value=ord(char))
for node in code.filter_text(recursive=False):
if char in node:
code.replace(node, node.replace(char, replacement))

def _blank_param_value(self, value): # TODO
pass
pass # MAKE VALUE CONTAIN ABSOLUTELY TWO TEXT NODES: FIRST IS SPACING BEFORE CHUNK AND SECOND IS SPACING AFTER CHUNK

@property
def name(self):
@@ -68,30 +74,31 @@ class Template(Node):

def add(self, name, value, showkey=None):
name, value = parse_anything(name), parse_anything(value)
surface_text = value.filter_text(recursive=False)
for node in surface_text:
value.replace(node, node.replace("|", "|"))
self._surface_escape(value, "|")

if self.has_param(name):
self.remove(name, keep_field=True)
existing = self.get(name)
if showkey is None: # Infer showkey from current value
showkey = existing.showkey
if not showkey:
self._surface_escape(value, "=")
nodes = existing.value.nodes
existing.value = parse_anything([nodes[0], value, nodes[1]])
return existing

if showkey is None:
if any(["=" in node for node in surface_text]):
showkey = True
try:
int(name)
except ValueError:
showkey = False
else:
try:
int(name)
except ValueError:
showkey = False
else:
showkey = True
elif not showkey:
for node in surface_text:
value.replace(node, node.replace("=", "|"))

if self.has_param(name):
self.remove_param(name, keep_field=True)
existing = self.get_param(name).value
self.get_param(name).value = value # CONFORM TO FORMATTING?
else:
self.params.append(Parameter(name, value, showkey)) # CONFORM TO FORMATTING CONVENTIONS?
showkey = True
if not showkey:
self._surface_escape(value, "=")
param = Parameter(name, value, showkey) # CONFORM TO FORMATTING CONVENTIONS?
self.params.append(param)
return param

def remove(self, name, keep_field=False): # DON'T MESS UP NUMBERING WITH show_key = False AND keep_field = False
name = name.strip() if isinstance(name, basestring) else unicode(name)


+ 5
- 5
mwparserfromhell/utils.py Näytä tiedosto

@@ -24,17 +24,17 @@ import mwparserfromhell
from mwparserfromhell.nodes import Node

def parse_anything(value):
Wikicode = mwparserfromhell.wikicode.Wikicode
if isinstance(value, Wikicode):
wikicode = mwparserfromhell.wikicode.Wikicode
if isinstance(value, wikicode):
return value
if isinstance(value, Node):
return Wikicode([value])
return wikicode([value])
if isinstance(value, basestring):
return mwparserfromhell.parse(value)
if isinstance(value, int):
return mwparserfromhell.parse(unicode(value))
if value is None:
return Wikicode([])
return wikicode([])
try:
nodelist = []
for item in value:
@@ -42,4 +42,4 @@ def parse_anything(value):
except TypeError:
error = "Needs string, Node, Wikicode, int, None, or iterable of these, but got {0}: {1}"
raise ValueError(error.format(type(value), value))
return Wikicode(nodelist)
return wikicode(nodelist)

+ 1
- 1
mwparserfromhell/wikicode.py Näytä tiedosto

@@ -134,7 +134,7 @@ class Wikicode(StringMixIn):
raise ValueError("Cannot coerce multiple nodes into one index")
if index >= len(self.nodes) or -1 * index > len(self.nodes):
raise IndexError("List assignment index out of range")
self.nodex.pop(index)
self.nodes.pop(index)
if nodes:
self.nodes[index] = nodes[0]



Ladataan…
Peruuta
Tallenna