Example #1
0
def test_split_to_array():
    "Note one_line behaviour in split_to_array."
    arg = trim("""

        = x
        = y
        y
        = z

        """)
    out = [('=', 'x'), ('=', 'y y'), ('=', 'z')]
    assert split_to_array(arg, '=') == out

    arg = trim("""
        + Heading
        """)
    out = [('+', 'Heading')]
    assert split_to_array(arg, '.%#+') == out

    arg = trim("""

        . x
        . y
        .
        .
        . z

        """)
    out = [('.', 'x'), ('.', 'y'), ('.', ''), ('.', ''), ('.', 'z')]
    assert split_to_array(arg, '.') == out
Example #2
0
def align_block(content, settings):
    """
    There is space around each set of alignment statements, but not
    between the individual lines. Ths means divs are wrapped in an outer
    paragraph tag.
    """
    lines = split_to_array(content, Config.aligns)
    out = []
    for char, line in lines:
        class_name = {
            '.': 'text-left',
            ';': 'text-center',
            ',': 'text-right',
            ':': 'indent',
            '~': 'indent-hanging',
        }.get(char, '')
        out += [tag('div', line, class_name)]
    if len(out) > 0:
        return "".join([
            '<div class="wr-align-block space">',
            '<span>',
            ''.join(out),
            '</span>',
            '</div>',
        ])
    else:
        return ''
Example #3
0
 def text(self):
     """
     Leave lines beginning with a control character unwrapped.
     """
     tuples = split_to_array(self.content, Config.all_control_chars)
     lines = [char + ' ' + one_line(line) for char, line in tuples]
     space_above = "\n" if self.control_character in Config.subheads else ""
     return space_above + "\n".join(lines)
Example #4
0
def table_block(text, settings):
    """
    Generate simple tables.
    """
    char = text[0]
    divisions = split_to_array(text, Config.tables, capture_characters=False)
    has_headers = (char == '!')
    data, options = parse_table_data(divisions, has_headers)
    return generate_table(data, options)
Example #5
0
def match_footnotes(text):
    """
    Find the footnote references in a block of text.
    """
    def join_lists(_):
        return [elem for sublist in _ for elem in sublist]

    footnotes = join_lists([
        split_to_array(_.content, '^', capture_characters=False)
        for _ in BlockList(text).find('CharacterBlock', '^')
    ])
    return {count: _ for count, _ in zip(Numbers(), footnotes)}
Example #6
0
def caption_block(content, settings):
    """
    = Caption
    """
    lines = split_to_array(content, prefixes=Config.caption)
    out = []
    for char, content in lines:
        if char == Config.caption:
            out += [tag('p', content, 'caption')]
    if len(out) > 0:
        return "\n".join(out)
    else:
        return ""
Example #7
0
def note_block(content, settings):
    """
    " block note
    """
    lines = split_to_array(content, prefixes=Config.notes)

    def note_line(part):
        char, text = part
        if char == '"':
            return tag('p', text, 'note')
        else:
            return alert(content)

    out = map(note_line, lines)
    return "\n".join(out)
Example #8
0
def subhead_block(content, settings):
    """
    Just one level of subheadings for now, no captions.
    @ Subheading
    """
    lines = split_to_array(content, prefixes=Config.subheads)

    def subhead_line(part):
        char, text = part
        if char == '@':
            return tag('p', text, 'subhead')
        else:
            return alert(content)

    out = map(subhead_line, lines)
    return "\n".join(out)
Example #9
0
def quote_block(content, settings):
    """
    Config.quotes:
    > blockquote
    plus:
    = caption
    """
    lines = split_to_array(content, prefixes=Config.quotes + Config.caption)
    out = []
    for char, content in lines:
        if char == '>':
            out += [tag('blockquote', content)]
        elif char == Config.caption:
            out += [tag('p', content, 'caption')]
    if len(out) > 0:
        return "\n".join(out)
    else:
        return ""
Example #10
0
def gloss_block(text, settings):
    """
    Build HTML from text.
    """
    inline = Inline()
    char = text[0]  # '/', as used here
    gloss = []
    num = None
    for _, line in split_to_array(text, char):
        parts = line.split(" %s " % char)
        if len(parts) == 1:
            num = parts.pop(0)
        else:
            source_html = inline.process(parts.pop(0))
            translations_html = [inline.process(part) for part in parts]
            if num is not None:
                gloss += [[(str(num), ''), (source_html, translations_html)]]
                num = None
            else:
                gloss += [[(source_html, translations_html)]]

    env = Environment(autoescape=True)
    tpl = env.from_string(
        trim("""
        <div class="gloss">
        {% for translation_group in gloss %}
            <div class="phrase-group">
                {% for source_html, translations in translation_group %}
                <div class="phrase">
                    <div class="source">{{ source_html|safe }}</div>
                    {% for translation_html in translations %}
                    <div class="translation">{{ translation_html|safe }}</div>
                    {% endfor %}
                </div>
                {% endfor %}
            </div>
        {% endfor %}
        </div>
    """))

    return tpl.render(gloss=gloss)
Example #11
0
def column_block(text, settings):
    """
    Simple columns (maximum of 4):

    ] Column 1
    ] Column 2

    # Or 12-column grids? @todo
    #
    # ] 3 ] Col 1.
    # ] 9 ] Column Number Two.
    """
    char = text[0]
    parts = split_to_array(text, prefixes=char)
    html = ''
    parts = parts[:4]  # <-- max. 4 parts
    twelfths = round(12 / len(parts))
    bootstrap_class = 'col-md-%d' % twelfths
    for char, content in parts:
        html += tag('p', content, 'float-left %s' % bootstrap_class)
    html += "<div style=\"clear: both\"></div>"
    return html