aeidon/tags/mpl2.py

Source code for module aeidon.tags.mpl2 from file aeidon/tags/mpl2.py.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
# -*- coding: utf-8 -*-

# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Text markup for the MPL2 format."""

import aeidon

__all__ = ("MPL2",)


class MPL2(aeidon.tags.MicroDVD):

    """
    Text markup for the MPL2 format.

    MPL2 contains the following markup tags, all of which appear at the
    beginning of the line and affect up to the end of the line. In addition,
    any MicroDVD markup can be used.

     * ``\\...`` (bold)
     * ``/....`` (italic)
     * ``_....`` (underline)
    """

    format = aeidon.formats.MPL2

    def bolden(self, text, bounds=None):
        """Return bolded `text`."""
        return self._style_mpl2(text, "\\", bounds)

    @property
    def italic_tag(self):
        """Regular expression for an italic markup tag."""
        return self._get_regex(r"(^/)|(\{[Yy]:i\})")

    def italicize(self, text, bounds=None):
        """Return italicized `text`."""
        return self._style_mpl2(text, "/", bounds)

    def _main_decode(self, text):
        """Return `text` with decodable markup decoded."""
        text = self._decode_b(text, r"<\\>(.*?)</\\>", 1)
        text = self._decode_i(text, r"</>(.*?)<//>", 1)
        text = self._decode_u(text, r"<_>(.*?)</_>", 1)
        return aeidon.tags.MicroDVD._main_decode(self, text)

    def _pre_decode(self, text):
        """Return `text` with markup prepared for decoding."""
        text = self._pre_decode_identify(text)
        return aeidon.tags.MicroDVD._pre_decode(self, text)

    def _pre_decode_identify(self, text):
        """
        Return `text` with all tags identified and closed.

        ``\\``, ``/`` and ``_`` characters at the beginnings of lines are
        identified as tags and replaced with artificial tags ``<\\>``, ``</>``
        and ``<_>``. Closing tags are added to the ends of lines as artificial
        tags ``</\\>``, ``<//>`` and ``</_>``.
        """
        lines = text.split("\n")
        re_tag = self._get_regex(r"^([\\/_]+)(.*)$")
        for i, line in enumerate(lines):
            match = re_tag.search(line)
            if match is None: continue
            lines[i] = match.group(2)
            for tag in reversed(match.group(1)):
                lines[i] = "<{}>{}</{}>".format(tag, lines[i], tag)
        return "\n".join(lines)

    def _style_mpl2(self, text, tag, bounds=None):
        """Return `text` wrapped in markup `tag`."""
        a, z = bounds or (0, len(text))
        prefix = text[:a].split("\n")[-1]
        suffix = text[z:].split("\n")[0]
        re_alpha = self._get_regex(r"\w")
        # Return plain text if bounds does not define an entire line or
        # subtitle and thus cannot be marked without side-effects.
        if re_alpha.search(prefix): return text
        if re_alpha.search(suffix): return text
        styled_text = text[a:z].replace("\n", "\n{}".format(tag))
        return "".join((text[:a], tag, styled_text, text[z:]))

    @property
    def tag(self):
        """Regular expression for any markup tag."""
        return self._get_regex(r"(^[\\/_]+)|(\{[CFSYcfsy]:.*?\})")

    def underline(self, text, bounds=None):
        """Return underlined `text`."""
        return self._style_mpl2(text, "_", bounds)