class a(inline): """ A hypertext link. """ xmlns = xmlns model = sims.ElementsOrText(inline) class Attrs(xsc.Element.Attrs): class href(xsc.URLAttr): pass class hreflang(xsc.TextAttr): pass def convert_docbook(self, converter): e = converter.target.link(self.content, linkend=self.attrs.href) return e.convert(converter) def convert_html(self, converter): e = converter.target.a(self.content, href=self.attrs.href, hreflang=self.attrs.hreflang) return e.convert(converter) def convert_fo(self, converter): if "href" in self.attrs: e = converter.target.basic_link( self.content, converter[self].linkattrs, external_destination=self.attrs.href) else: e = self.content return e.convert(converter)
class xref(inline): """ An internal cross reference. """ xmlns = xmlns model = sims.ElementsOrText(inline) class Attrs(xsc.Element.Attrs): class ref(xsc.TextAttr): pass def convert_docbook(self, converter): e = converter.target.link(self.content, linkend=self.attrs.ref) return e.convert(converter) def convert_html(self, converter): e = converter.target.a(self.content, href=("#", self.attrs.ref)) return e.convert(convertert) def convert_fo(self, converter): if "href" in self.attrs: e = converter.target.basic_link( self.content, converter[self].linkattrs, internal_destination=self.attrs.ref) else: e = self.content return e.convert(converter)
class dd(block): """ A wrapper for the elements of a list item :class:`dl`. """ xmlns = xmlns model = sims.ElementsOrText( block, inline ) # if it contains no block elements, the content will be promoted to a paragraph def convert_docbook(self, converter): if self[block]: content = self.content else: content = converter.target.para(self.content) e = converter.target.listitem(content) return e.convert(converter) def convert_html(self, converter): e = converter.target.dd(self.content) return e.convert(converter) def convert_fo(self, converter): target = converter.target context = converter[self] context.lists[-1][1] += 1 type = context.lists[-1][0] context.indentcount += 1 if self[block]: # Do we have a block in our content? content = self.content # yes => use the content as is else: content = p(self.content) # no => wrap it in a paragraph e = target.block(content, start_indent=context.indent()) context.indentcount -= 1 return e.convert(converter)
class app(inline): """ The name of a software program. """ xmlns = xmlns model = sims.ElementsOrText(rep) class Attrs(xsc.Element.Attrs): class moreinfo(xsc.URLAttr): pass def convert_docbook(self, converter): e = converter.target.application(self.content, moreinfo=self.attrs.moreinfo) return e.convert(converter) def convert_html(self, converter): if "moreinfo" in self.attrs: e = converter.target.a(self.content, class_="app", href=self.attrs.moreinfo) else: e = converter.target.span(self.content, class_="app") return e.convert(converter) def convert_fo(self, converter): if "moreinfo" in self.attrs: e = converter.target.basic_link( self.content, converter[self].linkattrs, external_destination=self.attrs.moreinfo) else: e = self.content return e.convert(converter)
class h(base): """ The text of the title of a :class:`section` or an :class:`example`. """ xmlns = xmlns model = sims.ElementsOrText(inline) def convert_docbook(self, converter): e = converter.target.title(self.content.convert(converter)) return e.convert(converter) def convert_html(self, converter): context = converter[self] if context.stack: if isinstance(context.stack[-1], example): e = self.content elif isinstance(context.stack[-1], section): level = len(context.sections) if context.firstheaderlevel is None: context.firstheaderlevel = level e = getattr(converter.target, f"h{context.firstheaderlevel+level}", converter.target.h6)(self.content) else: raise ValueError( f"unknown node {context.stack[-1]!r} on the stack") else: context.firstheaderlevel = 0 e = converter.target.h1(self.content) return e.convert(converter) def convert_fo(self, converter): e = self.content return e.convert(converter)
class meth(code): """ The name of a method or memberfunction in a programming language. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.methodname(self.content) return e.convert(converter)
class user(code): """ The name of a user account. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.literal(self.content, role="username") return e.convert(converter)
class dir(code): """ The name of a directory. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.filename(self.content, class_="directory") return e.convert(converter)
class prop(code): """ The name of a property in a programming language. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.varname(self.content, role="property") return e.convert(converter)
class const(code): """ The name of a constant. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.literal(self.content, role="constant") return e.convert(converter)
class exc(code): """ The name of an exception class. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.classname(self.content) return e.convert(converter)
class data(code): """ The name of a data object. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.literal(self.content, role="data") return e.convert(converter)
class z(inline): """ Put the content into double quotes. """ xmlns = xmlns model = sims.ElementsOrText(inline) def convert(self, converter): e = xsc.Frag("\u201c", self.content, "\u201d") return e.convert(converter)
class obj(code): """ A object of unspecified type. """ xmlns = xmlns model = sims.ElementsOrText(rep, self, cls) def convert_docbook(self, converter): e = converter.target.varname(self.content) return e.convert(converter)
class attr(code): """ The name of an attribute of a class/object. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.methodname(self.content) return e.convert(converter)
class func(code): """ The name of a function or subroutine, as in a programming language. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.function(self.content) return e.convert(converter)
class class_(code): """ The name of a class, in the object-oriented programming sense. """ xmlns = xmlns xmlname = "class" model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.classname(self.content) return e.convert(converter)
class mod(code): """ The name of a Python module. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.classname(self.content, role="module") return e.convert(converter) def convert_html(self, converter): e = converter.target.code(self.content, class_="module") return e.convert(converter)
class markup(code): """ A string of formatting markup in text that is to be represented literally. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.markup(self.content) return e.convert(converter) def convert_html(self, converter): e = converter.target.code(self.content, class_="markup") return e.convert(converter)
class option(code): """ An option for a software command. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.option(self.content) return e.convert(converter) def convert_html(self, converter): e = converter.target.code(self.content, class_="option") return e.convert(converter)
class lit(code): """ Inline text that is some literal value. """ xmlns = xmlns model = sims.ElementsOrText(code, rep) def convert_docbook(self, converter): e = converter.target.literal(self.content) return e.convert(converter) def convert_html(self, converter): e = converter.target.code(self.content, class_="lit") return e.convert(converter)
class file(code): """ The name of a file. """ xmlns = xmlns model = sims.ElementsOrText(rep) def convert_docbook(self, converter): e = converter.target.filename(self.content) return e.convert(converter) def convert_html(self, converter): e = converter.target.code(self.content, class_="filename") return e.convert(converter)
def test_elementsortext(): with xsc.Pool(): class el11(xsc.Element): xmlname = "el1" xmlns = "ns1" class el12(xsc.Element): xmlname = "el2" xmlns = "ns1" class el21(xsc.Element): xmlname = "el1" xmlns = "ns2" class el22(xsc.Element): xmlname = "el2" xmlns = "ns2" el11.model = sims.ElementsOrText(el11, el21) e = el11() e.bytes(validate=True) e = el11("foo") e.bytes(validate=True) e = el11(php.php("gurk")) e.bytes(validate=True) e = el11(xsc.Comment("gurk")) e.bytes(validate=True) e = el11(el11()) e.bytes(validate=True) e = el11(el21()) e.bytes(validate=True) e = el11(el12()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.WrongElementWarning) e = el11(el22()) with warnings.catch_warnings(record=True) as w: e.bytes(validate=True) assert len(w) == 1 assert issubclass(w[-1].category, sims.WrongElementWarning)
class li(block): """ A wrapper for the elements of a list item in :class:`ul` or :class:`ol`. """ xmlns = xmlns model = sims.ElementsOrText(block, inline) # if it contains no block elements, the content will be promoted to a paragraph def convert_docbook(self, converter): if self[block]: content = self.content else: content = converter.target.para(self.content) e = converter.target.listitem(content) return e.convert(converter) def convert_html(self, converter): e = converter.target.li(self.content) return e.convert(converter) def convert_fo(self, converter): target = converter.target context = converter[self] context.lists[-1][1] += 1 type = context.lists[-1][0] if type=="ul": label = "\u2022" elif type=="ol": label = "{}.".format(context.lists[-1][1]) context.indentcount += 1 if self[block]: # Do we have a block in our content? content = self.content # yes => use the content as is else: content = p(self.content) # no => wrap it in a paragraph e = target.list_item( target.list_item_label( target.block(label), start_indent=context.labelindent() ), target.list_item_body( content, start_indent=context.indent() ) ) context.indentcount -= 1 return e.convert(converter)
class dt(block): """ A term inside a :class:`dl`. """ xmlns = xmlns model = sims.ElementsOrText(inline) def convert_docbook(self, converter): e = converter.target.term(self.content) return e.convert(converter) def convert_html(self, converter): e = converter.target.dt(self.content) return e.convert(converter) def convert_fo(self, converter): e = converter.target.block(self.content, font_style="italic") return e.convert(converter)
class strong(inline): """ Emphasized text. """ xmlns = xmlns model = sims.ElementsOrText(inline) def convert_docbook(self, converter): e = converter.target.emphasis(self.content) return e.convert(converter) def convert_html(self, converter): e = converter.target.strong(self.content) return e.convert(converter) def convert_fo(self, converter): e = converter.target.inline(self.content, converter[self].strongattrs) return e.convert(converter)
class p(block): """ A paragraph. """ xmlns = xmlns model = sims.ElementsOrText(inline) class Attrs(xsc.Element.Attrs): class type(xsc.TextAttr): pass def convert_docbook(self, converter): e = converter.target.para(self.content, role=self.attrs.type) return e.convert(converter) def convert_html(self, converter): e = converter.target.p(self.content, class_=self.attrs.type) return e.convert(converter) def convert_fo(self, converter): e = fo.block(self.content, converter[self].vspaceattrs, line_height="130%") return e.convert(converter)
class pyref(inline): """ Reference to a Python object: module, class, method, property or function. """ xmlns = xmlns model = sims.ElementsOrText(inline) class Attrs(xsc.Element.Attrs): class module(xsc.TextAttr): pass class class_(xsc.TextAttr): xmlname = "class" class method(xsc.TextAttr): pass class property(xsc.TextAttr): pass class function(xsc.TextAttr): pass class Context(xsc.Element.Context): def __init__(self): xsc.Element.Context.__init__(self) self.base = "http://127.0.0.1:7464/" def convert(self, converter): target = converter.target context = converter[self] if target.xmlns == xmlns: # our own namespace return self.convert_doc(converter) if "function" in self.attrs: function = str(self.attrs.function.convert(converter)) else: function = None if "method" in self.attrs: method = str(self.attrs.method.convert(converter)) else: method = None if "property" in self.attrs: prop = str(self.attrs.property.convert(converter)) else: prop = None if "class" in self.attrs: class_ = str(self.attrs.class_.convert(converter)).replace( ".", "-") else: class_ = None if "module" in self.attrs: module = str(self.attrs.module.convert(converter)) if module.startswith("ll."): module = module[3:].replace(".", "/") elif module == "ll": module = "core" else: module = None else: module = None e = self.content if target.xmlns == html.xmlns: if function is not None: if module is not None: e = target.a(e, href=(context.base, module, "/index.html#", function)) elif method is not None: if class_ is not None and module is not None: e = target.a(e, href=(context.base, module, "/index.html#", class_, "-", method)) elif prop is not None: if class_ is not None and module is not None: e = target.a(e, href=(context.base, module, "/index.html#", class_, "-", prop)) elif class_ is not None: if module is not None: e = target.a(e, href=(context.base, module, "/index.html#", class_)) elif module is not None: e = target.a(e, href=(context.base, module, "/index.html")) return e.convert(converter)
class litblock(block): """ A literal text block (like source code or a shell session). """ xmlns = xmlns model = sims.ElementsOrText(inline) cssclass = "litblock" def convert_html(self, converter): target = converter.target e = target.pre(class_=self.cssclass) for child in self.content: child = child.convert(converter) if isinstance(child, xsc.Text): for c in child.content: if c == "\t": c = tab() e.append(c) else: e.append(child) return e.convert(converter) def convert_fo(self, converter): target = converter.target context = converter[self] context.indentcount += 1 e = target.block(context.vspaceattrs, context.codeattrs, text_align="left", line_height="130%", font_size="90%", start_indent=context.indent(), end_indent=context.indent()) collect = target.block() first = True for child in self.content: child = child.convert(converter) if isinstance(child, xsc.Text): for c in child.content: # We have to do the following, because FOP doesn't support the white-space property yet if c == " ": c = "\xa0" # transform spaces into nbsps if c == "\t": c = target.inline("\u25ab\xa0\xa0\xa0", color="rgb(50%, 50%, 50%)") if c == "\n": if not collect and not first: # fix empty lines (but not the first one) collect.append("\ufeff") collect[ "line_height"] = "60%" # reduce the line-height e.append(collect) collect = target.block() first = False else: collect.append(c) else: collect.append(child) if collect: e.append(collect) context.indentcount -= 1 return e.convert(converter)
class type(xsc.TextAttr): pass class zeroOrMore(base): """ There can be zero or more recurrence of the enclosed pattern. """ xmlns = xmlns # modeltype definitions anyName.model = sims.Elements(except_) attribute.model = sims.ElementsOrText(choice, data, empty, externalRef, grammar, group, interleave, list, mixed, name, notAllowed, nsName, oneOrMore, optional, parentRef, ref, text, value, zeroOrMore) choice.model = sims.Elements(anyName, attribute, choice, data, element_, empty, externalRef, grammar, group, interleave, list, mixed, name, notAllowed, nsName, oneOrMore, optional, parentRef, ref, text, value, zeroOrMore) data.model = sims.Elements(except_, param) define.model = sims.Elements(attribute, choice, data, element_, empty, externalRef, grammar, group, interleave, list, mixed, notAllowed, oneOrMore, optional, parentRef, ref, text, value, zeroOrMore) div.model = sims.Elements(define, div, include, start) element_.model = \ except_.model = sims.Elements(anyName, attribute, choice, data, element_, empty, externalRef, grammar, group, interleave, list, mixed, name, notAllowed, nsName, oneOrMore, optional, parentRef, ref, text, value, zeroOrMore) grammar.model = sims.Elements(start, define, div, include)