예제 #1
0
    def parse(self,
              text_to_parse,
              ignore_macros=None,
              ignore_fatal_errors=False,
              verbose=False):

        # Create the real parser.
        parser = wmlparser.Parser(self.datadir, self.userdir)
        parser.verbose = verbose
        parser.gettext = self.gettext
        parser.macros = copy.copy(self.core_macros)

        #parser.verbose = True

        # Suppress complaints about undefined terrain macros
        parser.set_macro_not_found_callback(
            lambda wmlparser, name, params: name.startswith(
                "TERRAIN") or name == "DISABLE_TRANSITIONS")

        if ignore_macros:
            parser.macro_callback = ignore_macros

        # Create a WML root element and parse the given text into it.
        WML = wmldata.DataSub("WML")

        parser.parse_text(text_to_parse)

        parser.parse_top(WML)

        return WML
예제 #2
0
    def decode_WML(self, data):
        p = wmlparser.Parser(None, no_macros_in_string=True)
        p.verbose = False
        p.do_preprocessor_logic = True
        p.no_macros = True
        p.parse_text(data, binary=True)
        doc = wmldata.DataSub("WML")
        p.parse_top(doc)

        return doc

        def done():
            return pos[0] >= len(data)

        def next():
            c = data[pos[0]]
            pos[0] += 1
            return c

        def literal():
            s = pos[0]
            e = data.find("\00", s)

            pack = data[s:e]

            pack = pack.replace("\01\01", "\00")
            pack = pack.replace("\01\02", "\01")

            pos[0] = e + 1

            return pack

        while not done():
            code = ord(next())
            if code == 0:  # open element (name code follows)
                open_element = True
            elif code == 1:  # close current element
                tag.pop()
            elif code == 2:  # add code
                self.words[self.wordcount] = literal()
                self.wordcount += 1
            else:
                if code == 3:
                    word = literal()  # literal word
                else:
                    word = self.words[code]  # code
                if open_element:  # we handle opening an element
                    element = wmldata.DataSub(word)
                    tag[-1].insert(element)  # add it to the current one
                    tag.append(element)  # put to our stack to keep track
                elif word == "contents":  # detect any binary attributes
                    binary = wmldata.DataBinary(word, literal())
                    tag[-1].insert(binary)
                else:  # others are text attributes
                    text = wmldata.DataText(word, literal())
                    tag[-1].insert(text)
                open_element = False

        return WML
예제 #3
0
    def __init__(self, isocode, datadir, userdir, transdir):
        self.datadir = datadir
        self.userdir = userdir
        # Handle translations.
        self.translations = wmltools.Translations(transdir)

        def gettext(textdomain, x):
            return self.translations.get(textdomain, isocode, x, x)

        self.gettext = gettext

        # Create a new parser for the macros.
        parser = wmlparser.Parser(datadir)
        parser.gettext = self.gettext

        # Parse core macros.
        parser.parse_text("{core/macros/}\n")
        parser.parse_top(None)
        self.core_macros = parser.macros