Example #1
0
 def append(item):
     nonlocal tr, td
     if td == None:
         if tr == None:
             tr = Element(table, "tr")
             table.children.append("\n")
             table.children.append(tr)
         td = Element(tr, "td")
         tr.children.append(td)
     if isinstance(item, Element):
         if item.tag == "td":
             for child in td.children:
                 if isinstance(child, Element):
                     child.parent = item
             item.children = td.children + item.children
             for name, value in item.attrs.attrs:
                 td.attrs[name] = value
             item.attrs = td.attrs
             item.parent = tr
             tr.children[tr.children.index(td)] = item
             td = item
         else:
             item.parent = td
             td.children.append(item)
     else:
         item = strip(item)
         if len(item) > 0:
             td.children.append(item)
Example #2
0
    def test_setAttribute(self):
        foo = Element('foo')

        # Using standard DOM methods
        foo.setAttribute('alpha', '1')
        self.assertEqual('1', foo.getAttribute('alpha'))

        # Using modified __setitem__ and __getitem__
        foo['bravo'] = '2'
        self.assertEqual('2', foo['bravo'])
Example #3
0
 def append_toc(self, node):
     node.children.append("\n")
     node.children.append("\n")
     ol = Element(node, "ol", [("start", "0")], ["\n"])
     for page in self.page_list:
         title = page["title"]
         file_name = page["file_name"]
         li = Element(ol, "li")
         if file_name == self.file_name:
             li.children.append(title + "(このページ)")
             li.attrs.appendClass("current")
         else:
             li.children.append(
                 Element(li, "a", [("href", file_name + ".html")], [title]))
         ol.children.append(li)
         ol.children.append("\n")
     node.children.append(ol)
     node.children.append("\n")
Example #4
0
    def test_append(self):
        div1 = Element('div')
        div2 = Element('div')
        div1.append(div2)
        self.assertEqual(1, len(div1))

        childElement = div1[0]
        self.assertTrue(div2 is childElement)

        p = Element('p')
        p.append('some text')
        self.assertEqual(1, len(p))

        textNode = p[0]
        self.assertTrue(isinstance(textNode, TextNode))
Example #5
0
    def __init__(self, parent = None, options = None, value = None, **kwargs):
        Element.__init__(self, parent = parent, **kwargs)

        options = [] if options is None else options 
        
        if value:
            if value in options:
                self.value = value
            else:
                raise XhtmlError, "Value is not in list of options"
        else:
            if len(options) > 0:
                self.value = options[0]
            else:
                self.value = None
            
        for an_option in options:
            option(parent = self, text = an_option)
            
        self.event_osc = events.Change(parent = self)
Example #6
0
    def test_getitem(self):
        className = 'description'
        attributes = {'class': className}
        div = Element('div', **attributes)

        p = Element('p')
        div.append(p)

        href = 'http://www.python.org'
        a = Element('a', href=href)
        div.append(a)

        self.assertIs(p, div[0])
        self.assertEqual(div['class'], 'description')
        self.assertIs(a, div[1])
        self.assertEqual(href, div[1]['href'])
Example #7
0
    def __init__(self, source_dir, main_title, namespace, libs, page_list,
                 aliases, page_num, output_dir):
        super().__init__()
        self.aliases = aliases
        self.root = Element(None, None, [])
        self.curElem = self.root

        self.ids = set()
        self.id_num = 0
        self.in_form = False

        self.examples = []
        self.example_cur = None
        self.example_prev = None

        self.page_num = page_num
        self.page_list = page_list
        self.page = page_list[page_num]
        self.file_name = self.page["file_name"]
        self.file_name_src = source_dir.joinpath(self.file_name + ".src.html")
        self.file_name_html = output_dir.joinpath(self.file_name + ".html")
        self.file_name_js = self.file_name + ".js"
        self.file_name_ts = output_dir.joinpath(self.file_name + ".ts")
        self.main_title = main_title
        self.sub_title = self.page["title"]
        self.title = main_title + " >> " + str(
            page_num) + ". " + self.sub_title

        self.ts = Script()
        lib_dir = source_dir.relative_to(output_dir.resolve())
        for lib in libs:
            self.ts.append(
                f"/// <reference path=\"{lib_dir.joinpath(lib)}\"/>")
        self.ts.append("\n")
        self.ts.append(f"namespace {namespace}{{")

        self.tab_index = 0
Example #8
0
 def __init__(self, parent, value = "", **kwargs):
     Element.__init__(self, parent = parent, value = value, **kwargs)
Example #9
0
    def test_toprettyxml(self):
        svg = Element('svg')
        self.assertEqual('<svg/>', svg.toprettyxml())

        svg.setAttribute('width', '200')
        self.assertEqual('<svg width="200"/>', svg.toprettyxml())

        p = Element('p')
        p.append('some text')
        self.assertEqual('<p>some text</p>', p.toprettyxml())

        div = Element('div')

        p = Element('p')
        p.append('one')
        div.append(p)
        self.assertEqual('<div>\n\t<p>one</p>\n</div>', div.toprettyxml())

        p = Element('p')
        p.append('two')
        div.append(p)

        self.assertEqual('<div>\n\t<p>one</p>\n\t<p>two</p>\n</div>',
                         div.toprettyxml())
Example #10
0
    def arrange_table(self, node):
        table = Element(node, "table", [])
        tr = None
        td = None

        aligns = {}
        align_data = node.attrs.pop("data-align")
        if align_data != None:
            for i, a in enumerate(align_data):
                if a == "r":
                    aligns[i] = "right"
                elif a == "c":
                    aligns[i] = "center"

        def strip(item):
            item = item.strip()
            if len(item) > 0 and item[-1] == "\\":
                item += " "
            return item

        def append(item):
            nonlocal tr, td
            if td == None:
                if tr == None:
                    tr = Element(table, "tr")
                    table.children.append("\n")
                    table.children.append(tr)
                td = Element(tr, "td")
                tr.children.append(td)
            if isinstance(item, Element):
                if item.tag == "td":
                    for child in td.children:
                        if isinstance(child, Element):
                            child.parent = item
                    item.children = td.children + item.children
                    for name, value in item.attrs.attrs:
                        td.attrs[name] = value
                    item.attrs = td.attrs
                    item.parent = tr
                    tr.children[tr.children.index(td)] = item
                    td = item
                else:
                    item.parent = td
                    td.children.append(item)
            else:
                item = strip(item)
                if len(item) > 0:
                    td.children.append(item)

        for child in node.children:
            if isinstance(child, Element):
                append(child)
            else:
                items = child.split("&")
                for i, item in enumerate(items):
                    items2 = item.split("\\\\")
                    if len(items2) > 0:
                        for j, item2 in enumerate(items2):
                            if j > 0:
                                tr = None
                                td = None
                            append(item2)
                    else:
                        append(item)

                    if i < len(items) - 1:
                        td = None
        row_count = max(
            len(tr.children) if isinstance(tr, Element) else 0
            for tr in table.children)
        for tr in table.children:
            if not isinstance(tr, Element):
                continue
            i = 0
            for td in tr.children:
                if not isinstance(td, Element):
                    continue
                new_children = []
                prev_is_string = False
                for child in td.children:
                    if isinstance(child, str):
                        if prev_is_string:
                            new_children[-1] += child
                        else:
                            new_children.append(child)
                        prev_is_string = True
                    else:
                        new_children.append(child)
                        prev_is_string = False
                if len(new_children) > 0:
                    first = new_children[0]
                    if isinstance(first, str):
                        if len(first) > 0 and first[0] == "$":
                            new_children[0] = first[1:]
                        else:
                            new_children[0] = "$ \displaystyle " + first
                    else:
                        new_children.insert(0, "$ \displaystyle ")
                    last = new_children[-1]
                    if isinstance(last, str):
                        if len(last) > 0 and last[-1] == "$":
                            new_children[-1] = last[:-1]
                        else:
                            new_children[-1] = last + "$"
                    else:
                        new_children.append("$")
                tmp_children = []
                for child in new_children:
                    if isinstance(child, Element) or child.strip() != "":
                        tmp_children.append(child)
                new_children = tmp_children
                td.children = new_children
                if i in aligns:
                    td.attrs.appendClass("" + aligns[i])
                i += 1
            for i in range(row_count - len(tr.children)):
                tr.children.append(Element(tr, "td"))
        table.children.append("\n")
        node.children = ["\n", table, "\n"]
        node.attrs.appendClass("demo-equation")
Example #11
0
 def __init__(self, parent, type = None, value = None, text = None, **kwargs):
     Element.__init__(self, parent = parent, text = text, type = type, value = value, **kwargs)
Example #12
0
 def __init__(self, parent, src = None, alt = None, height = None, width = None, longdesc = None, **kwargs):
     Element.__init__(self, parent = parent, src = src, alt = alt, height = height, width = width, longdesc = longdesc, **kwargs)
Example #13
0
    def test_hasElementChildren(self):
        div = Element('div')
        div.append('foo')
        self.assertEqual(False, div.hasElementChild())

        div = Element('div')
        div.append('text child')
        div.append(Element('hr'))
        self.assertEqual(True, div.hasElementChild())
Example #14
0
    def modify(self, node, prev_cls=None):
        try:
            if node.tag in self.aliases:
                alias = self.aliases[node.tag]
                node.tag = alias["tag"]
                node.attrs.appendClass(alias["class"])
            cls = self.check_demo_class(node)
            if cls != None:
                if cls == "example":
                    self.ensure_id(node)
                    if self.example_cur != None:
                        self.example_cur = Example(self, node,
                                                   self.example_cur)
                    elif self.example_prev == None:
                        self.example_cur = Example(self, node)
                        self.examples.append(self.example_cur)
                    else:
                        self.example_cur = self.example_prev
                        self.example_cur.append_element(node)
                elif cls == "table":
                    self.arrange_table(node)
                elif cls == "title":
                    node.children.append(self.title)
                elif cls == "sub_title":
                    node.children.append(f"{self.page_num}. {self.sub_title}")
                elif cls == "main_title":
                    node.children.append(self.main_title)
                elif cls == "toc":
                    self.append_toc(node)
                else:
                    ex = self.get_current_example()
                    if cls == "submit":
                        ex.set_submit_button(node)
                    elif cls == "form":
                        if self.in_form:
                            raise Exception("formが2重になっています")
                        self.in_form = True
                        ex.start_form(node)
                    elif cls == "init":
                        ex.append_init(node)
                    elif cls == "calc":
                        ex.append_calc(node)
                    elif cls == "if":
                        ex.start_if(node)
                    elif cls == "else":
                        if prev_cls != "if":
                            raise Exception("elseの直前にifがありません")
                        ex.start_else(node)
                    elif cls == "inject":
                        ex.append_injection(node)
                    else:
                        print(node.parent.tag, node.parent.attrs,
                              node.parent.children)
                        raise Exception(f"不正なclass: {cls}")
            elif self.in_form:
                if node.tag == "input":
                    if cls != None:
                        print(node.parent.tag, node.parent.attrs,
                              node.parent.children)
                        raise Exception(f"不正なclass: {cls}")
                    self.get_current_example().append_input(node)

            prev_cls = None
            for child in list(node.children):
                if isinstance(child, Element):
                    prev_cls = self.modify(child, prev_cls)

            if cls == "example":
                self.example_prev = self.example_cur
                self.example_cur = self.get_current_example().parent
            elif cls == "if":
                self.get_current_example().end_if(self.curElem)
            elif cls == "else":
                self.get_current_example().end_else(self.curElem)
            elif cls == "form":
                self.in_forn = False

            if node.tag == "head":
                node.children.append(
                    Element(node, "script", [["src", self.file_name_js]]))
                node.children.append("\n")
            elif node.tag == "form":
                self.in_form = False

            return cls


#		except NotInExample:
#			if cls != None:
#				print(f"Example外で使われました: <{node.tag} class=\"demo-{cls}\"> in {node.pos}")
#			else:
#				print(f"Example外で使われました: <{node.tag}> in {node.pos}")
#			exit()
        finally:
            pass
Example #15
0
 def __init__(self, parent = None, href = None, text = None, **kwargs):
     Element.__init__(self, parent = parent, text = text, href = href, **kwargs)
Example #16
0
 def __init__(self, parent, type, language, src = None, **kwargs):
     Element.__init__(self, parent = parent, type = type, language = language, src = src, **kwargs)
Example #17
0
 def __init__(self, parent, href, rel, type, **kwargs):
     Element.__init__(self, parent = parent, rel = rel, type = type, href = href, **kwargs)
Example #18
0
 def __init__(self, parent, text):
     Element.__init__(self, parent = parent, text = text)
     if text == parent.value:
         self.att.selected = "yes"
Example #19
0
 def handle_starttag(self, tag, attrs):
     elem = Element(self.curElem, tag, attrs, [], self.getpos())
     self.curElem.children.append(elem)
     self.curElem = elem
Example #20
0
 def __init__(self, parent = None, text = None, title = None, **kwargs):
     Element.__init__(self, parent = parent, text = text, title = title, **kwargs)