Skip to content

Commit

Permalink
This project now uses HTML as the base languages instead of XML.
Browse files Browse the repository at this point in the history
XML is too strict.
  • Loading branch information
drhodes committed Dec 22, 2024
1 parent c620dcd commit 95d962b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
10 changes: 9 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ build-backend = "setuptools.build_meta"
[project]
name = "auxml"
version = "0.1.0"
dependencies = [ "lxml", "pytest", "pudb", "pytest-watcher", "xmldiff", "livereload" ]
dependencies = [
"lxml",
"pytest",
"pudb",
"pytest-watcher",
"xmldiff",
"livereload",
"beautifulsoup4",
]
requires-python = ">=3.8"
authors = [ {name = "Derek A. Rhodes", email = "[email protected]"} ]
description = "An author friendly macro system for XML"
Expand Down
8 changes: 5 additions & 3 deletions src/auxml/macro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from auxml.macro import MacroDef, MacroCall
from auxml.util import *
from auxml.html_tags import is_an_html_tag
from auxml.parser import parse_xml_file
from auxml.parser import parse_html_file

from auxml.err import SyntaxErrorAuXML

Expand All @@ -17,7 +17,7 @@ def register_directive(self, name, cls):
self.directives[name] = cls

def load_macro_file(self, infile):
macros = parse_xml_file(infile)
macros = parse_html_file(infile)
for el in macros.findall(".//define-macro"):
md = MacroDef(el)
self.add_macro_def(md)
Expand All @@ -28,7 +28,9 @@ def add_macro_def(self, macdef):
if macdef.name in self.macro_defs:
raise Exception(f"macro already defined: {macdef.name}")
if len(macdef.el.getchildren()) > 1:
raise SyntaxErrorAuXML(f"macro `{macdef.name}` may not have more than one child element")
msg = f"macro `{macdef.name}` may not have more than one child element\n"
msg += f"Got `{macdef.el.getchildren()}` child elements\n"
raise SyntaxErrorAuXML(msg)
self.macro_defs[macdef.name] = macdef

def cant_find(self, tag):
Expand Down
2 changes: 1 addition & 1 deletion src/auxml/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self):
mm.load_macro_file(cmdline.macros())

fname = cmdline.infile()
root = parse_xml_file(fname)
root = parse_html_file(fname)

exp = mm.expand_all(fname, root)
self.save(exp)
Expand Down
14 changes: 8 additions & 6 deletions src/auxml/parser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from lxml import etree

from bs4 import BeautifulSoup
_html_parser = etree.HTMLParser(remove_blank_text=True, remove_comments=True)
_xml_parser = etree.XMLParser(remove_blank_text=True, remove_comments=True)

def parse_html_file(fname):
tree = etree.parse(fname, _html_parser)
return tree.getroot()
text = open(fname).read()
soup = BeautifulSoup(text, "html.parser")
tree = etree.fromstring(str(soup), _xml_parser)
return tree

def parse_xml_file(fname):
tree = etree.parse(fname, _xml_parser)
return tree.getroot()
# def parse_xml_file(fname):
# tree = etree.parse(fname, _xml_parser)
# return tree.getroot()

6 changes: 5 additions & 1 deletion tests/macro_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ def test_mac_multi_rearrange_bug():
e = '''<div><div>A</div>B</div>'''
#pudb.set_trace()
with_mm([m], c, e)


def with_mm_file(filename):
mm = MacroManager()
mm.load_macro_file(filename)

def test_mac_multi_els_with_html_bug():
with_mm_file("tests/macrofile1.html")
9 changes: 9 additions & 0 deletions tests/macrofile1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<macros>
<define-macro name="weird">
<head>
<div>&</div>
<div></div>
</head>
</define-macro>
</macros>

0 comments on commit 95d962b

Please sign in to comment.