• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

earwig / mwparserfromhell / 7406365748

04 Jan 2024 06:39AM UTC coverage: 99.202%. Remained the same
7406365748

push

github

earwig
release/0.6.6

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

2984 of 3008 relevant lines covered (99.2%)

9.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/mwparserfromhell/parser/__init__.py
1
# Copyright (C) 2012-2020 Ben Kurtovic <ben.kurtovic@gmail.com>
2
#
3
# Permission is hereby granted, free of charge, to any person obtaining a copy
4
# of this software and associated documentation files (the "Software"), to deal
5
# in the Software without restriction, including without limitation the rights
6
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
# copies of the Software, and to permit persons to whom the Software is
8
# furnished to do so, subject to the following conditions:
9
#
10
# The above copyright notice and this permission notice shall be included in
11
# all copies or substantial portions of the Software.
12
#
13
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
# SOFTWARE.
20

21
"""
10✔
22
This package contains the actual wikicode parser, split up into two main
23
modules: the :mod:`.tokenizer` and the :mod:`.builder`. This module joins them
24
together into one interface.
25
"""
26

27
from .builder import Builder
10✔
28
from .errors import ParserError
10✔
29

30
try:
10✔
31
    from ._tokenizer import CTokenizer
10✔
32

UNCOV
33
    use_c = True
5✔
34
except ImportError:
5✔
35
    from .tokenizer import Tokenizer
5✔
36

37
    CTokenizer = None
5✔
38
    use_c = False
5✔
39

40
__all__ = ["use_c", "Parser", "ParserError"]
10✔
41

42

43
class Parser:
10✔
44
    """Represents a parser for wikicode.
45

46
    Actual parsing is a two-step process: first, the text is split up into a
47
    series of tokens by the :class:`.Tokenizer`, and then the tokens are
48
    converted into trees of :class:`.Wikicode` objects and :class:`.Node`\\ s
49
    by the :class:`.Builder`.
50

51
    Instances of this class or its dependents (:class:`.Tokenizer` and
52
    :class:`.Builder`) should not be shared between threads. :meth:`parse` can
53
    be called multiple times as long as it is not done concurrently. In
54
    general, there is no need to do this because parsing should be done through
55
    :func:`mwparserfromhell.parse`, which creates a new :class:`.Parser` object
56
    as necessary.
57
    """
58

59
    def __init__(self):
10✔
60
        if use_c and CTokenizer:
10✔
UNCOV
61
            self._tokenizer = CTokenizer()
5✔
62
        else:
63
            from .tokenizer import Tokenizer
10✔
64

65
            self._tokenizer = Tokenizer()
10✔
66
        self._builder = Builder()
10✔
67

68
    def parse(self, text, context=0, skip_style_tags=False):
10✔
69
        """Parse *text*, returning a :class:`.Wikicode` object tree.
70

71
        If given, *context* will be passed as a starting context to the parser.
72
        This is helpful when this function is used inside node attribute
73
        setters. For example, :class:`.ExternalLink`\\ 's
74
        :attr:`~.ExternalLink.url` setter sets *context* to
75
        :mod:`contexts.EXT_LINK_URI <.contexts>` to prevent the URL itself
76
        from becoming an :class:`.ExternalLink`.
77

78
        If *skip_style_tags* is ``True``, then ``''`` and ``'''`` will not be
79
        parsed, but instead will be treated as plain text.
80

81
        If there is an internal error while parsing, :exc:`.ParserError` will
82
        be raised.
83
        """
84
        tokens = self._tokenizer.tokenize(text, context, skip_style_tags)
10✔
85
        code = self._builder.build(tokens)
10✔
86
        return code
10✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc