Example #1
0
def test_replace():
    """
    Includes testing link creation.
    """
    parts = {
        'index':
        trim("""
            Test document

            - The Simple Test
            - The More Complex Test
            """),
        'test':
        trim("""
            This is a @[simple] test.

            This is a @[-more complex] test.
            """),
    }

    outline = Outline(parts, default_counters())
    placeholders = CrossReferences(parts, outline)

    tokenized = placeholders.insert(parts)
    html_parts = placeholders.replace(tokenized)
    out = html_parts['test']

    assert 'The Simple Test' in out
    assert '&sect;2' in out  # <-- complex uses shorthand
    assert 'the-simple-test' in out
    assert 'the-more-complex-test' in out
Example #2
0
def test_constructor():
    parts = sample_doc()
    outline = Outline(parts, default_counters())
    footnotes = Footnotes(parts, outline, 'prefix_')
    assert footnotes.outline.errors == {
        'topic-four': [('', 'More <kbd>^[link]s</kbd> than footnotes! (+1)')],
        'topic-three': [('', 'More footnotes than <kbd>^[link]s</kbd>! (+1)')]
    }
Example #3
0
def test_elements():
    arg = {
        'blog-1': "Blog One\n\n" + ('word ' * 500),
        'blog-2': "Blog Two\n\n" + ('word ' * 500)
    }
    outline = Outline(arg, default_counters())
    # Numbering, slug, title, title_slug, word count.
    expected = [(['1'], 'blog-1', 'Blog One', 'blog-one', 502),
                (['2'], 'blog-2', 'Blog Two', 'blog-two', 502)]
    assert outline.elements == expected
Example #4
0
def test_get_count():
    parts = {
        'index': 'INDEX',
        'other': 'OTHER',
    }
    outline = Outline(parts, default_counters())
    index = Index(outline)
    assert index.get_count('index') == 'i'
    assert index.get_count('index') == 'ii'
    assert index.get_count('other') == 'i'
    assert index.get_count('other') == 'ii'
Example #5
0
def test_footnotes():
    """
    Simple count that we have the right number of footnotes.
    """
    parts = sample_doc()
    footnotes = Footnotes(parts, Outline(parts, default_counters()), id_prefix)
    links = Links(footnotes)
    new_parts = links.insert(parts)
    end_parts = links.replace(new_parts)
    out = footnotes.html()
    dom = html.fragment_fromstring(out, create_parent='body')[0]
    assert len(dom.cssselect('sup')) == 6
Example #6
0
def test_tag():
    parts = {}
    outline = Outline(parts, default_counters())
    index = Index(outline)
    link = index.tag('alias', 'tag', 'subtag', '?', ['1', 'a'], 'i')

    assert '<a id="tag_subtag_1.a_i" href="#ref_tag_subtag_1.a_i">' in link
    assert 'alias?<sup>i</sup>' in link  # <-- (?)
    assert '</a>' in link

    back_link = '<a id="ref_tag_subtag_1.a_i" href="#tag_subtag_1.a_i">'
    assert back_link in index.tags['tag']['subtag']['1.a'][0]
Example #7
0
def test_get_count():
    parts = {
        'index': 'INDEX',
        'other': 'OTHER',
    }
    outline = Outline(parts, default_counters())
    bibliography = Bibliography(parts, outline, id_prefix)
    assert bibliography.get_count('index') == '*'
    assert bibliography.get_count('index') == '†'
    assert bibliography.get_count('index') == '‡'
    assert bibliography.get_count('other') == 'a'
    assert bibliography.get_count('other') == 'b'
    assert bibliography.get_count('other') == 'c'
Example #8
0
def test_match():
    parts = {
        'biblio':
        trim("""
            Author. 1999. Title #1. Publisher, City.
            Author. 2000. Title #1. Publisher, City.
            Author. 2000. Title #2. Publisher, City.
            """),
    }
    outline = Outline(parts, default_counters())
    bibliography = Bibliography(parts, outline, id_prefix)
    assert bibliography.match('author') == "Author. 1999."
    assert bibliography.match('author 2000') == "Author. 2000a."
Example #9
0
def test_html_simplest():
    parts = {
        'biblio':
        trim("""
            Author. 1999. Title #1. Publisher, City.
            Author. 2000. Title #1. Publisher, City.
            Author. 2000. Title #2. Publisher, City.
            """),
    }
    outline = Outline(parts, default_counters())
    bibliography = Bibliography(parts, outline, id_prefix)
    out = bibliography.html()
    dom = html.fragment_fromstring(out, create_parent='body')[0]
    assert len(dom.cssselect('div.indent-hanging')) == 3
Example #10
0
def test_citation():
    parts = {}
    outline = Outline(parts, default_counters())
    bibliography = Bibliography(parts, outline, id_prefix)
    link = bibliography.citation('Author /Title/', 'p.34', '.',
                                 'Author. 2000.', ['1', 'a'], 3)

    assert '<a id="PREFIX_author-2000_1.a_3" ' + \
           'href="#ref_PREFIX_author-2000_1.a_3">' in link
    assert '(Author <em>Title</em>, p.34).' in link  # <-- (?)
    assert '<sup>3</sup>' in link
    assert '</a>' in link

    back_link = '<a id="ref_PREFIX_author-2000_1.a_3" ' + \
                'href="#PREFIX_author-2000_1.a_3">'
    assert back_link in bibliography.citations['Author. 2000.']['1.a'][0]
Example #11
0
def test_constructor():
    parts = {
        'index':
        trim("""
            My Document!

            It contains a reference to ~[Chapman, /Conversation/, p.34].
        """),
        'biblio':
        trim("""
            Chapman, Nigel. 2014. Private conversation.
            Chapman, Nigel. 2014. Private conversation. #2.
        """),
    }

    outline = Outline(parts, default_counters())
    bibliography = Bibliography(parts, outline, id_prefix)
    citations = Citations(bibliography)

    assert bibliography.entries == SortedDict({
        "Chapman, Nigel. 2014a.":
        "Private conversation.",
        "Chapman, Nigel. 2014b.":
        "Private conversation. #2.",
    })

    new_parts = citations.insert(parts)

    assert new_parts == {
        'index':
        trim("""
            My Document!

            It contains a reference to %scitation:1%s
            """) % (DELIMITER, DELIMITER),  # ^ note the missing fullstop
        'biblio':
        trim("""
            Chapman, Nigel. 2014. Private conversation.
            Chapman, Nigel. 2014. Private conversation. #2.
            """),
    }

    end_parts = citations.replace(new_parts)
    assert '<em>Conversation</em>' in end_parts['index']
    assert 'p.34' in end_parts['index']
    dom = html.fragment_fromstring(end_parts['index'], create_parent='body')[0]
    assert len(dom.cssselect('a')) == 1
Example #12
0
def test_find_numbering():
    arg = {
        'index':
        trim("""
            Title

            - Chapter One
            - - Chapter One Aye
            - Chapter Two
            """),
        'chapter-one':
        "Chapter One\n\n" + ('word ' * 998)
    }
    outline = Outline(arg, default_counters())
    assert outline.find_numbering('chapter-one') == ['1']
    assert outline.find_numbering('chapter-one-aye') == ['1', 'a']
    assert outline.find_numbering('chapter-two') == ['2']
Example #13
0
def test_outline():
    arg = {
        'index':
        trim("""
            Title

            - Chapter One
            - Chapter Two
            """),
        'chapter-one':
        "Chapter One\n\n" + ('word ' * 998)
    }
    outline = Outline(arg, default_counters())
    out = outline.html('/')  # <-- edit_base_uri
    # @todo: Check this.
    # dom = html.fragment_fromstring(out, create_parent='body')[0]
    # assert len(dom.cssselect('tr')) == 4
    rows = findall('<tr', out)
    assert len(rows) == 4
Example #14
0
def test_constructor():
    parts = {
        'index':
        trim("""
            My Document!

            It contains a #[Tag] and a %[Tag].

            ` Part One
        """),
        'part-one':
        trim("""
            Or an #[alias: Tag, subtag]?
        """),
    }

    outline = Outline(parts, default_counters())
    index = Index(outline)
    tags = Tags(index)

    new_parts = tags.insert(parts)

    assert new_parts == {
        'index':
        trim("""
            My Document!

            It contains a %stag:1%s and a %stag:2%s

            ` Part One
            """) % (DELIMITER, DELIMITER, DELIMITER, DELIMITER),
        'part-one':
        trim("""
            Or an %stag:1%s
            """) % (DELIMITER, DELIMITER),
    }

    end_parts = tags.replace(new_parts)

    assert 'Tag<sup>i</sup>' in end_parts['index']
    assert 'Tag.<sup>ii</sup>' in end_parts['index']
Example #15
0
def test_html_simplest():
    parts = {
        'index':
        trim("""
            My Document!

            It contains a #[Tag] and a %[Tag].

            ` Part One
        """),
        'part-one':
        trim("""
            Or an #[alias: Tag, subtag]?
        """),
    }
    outline = Outline(parts, default_counters())
    index = Index(outline)
    index.tags = {'tag': {'subtag': {'1.1': ['LINK']}}}
    out = index.html()
    dom = html.fragment_fromstring(out, create_parent='body')[0]
    assert len(dom.cssselect('div.indent-first-line')) == 1
    assert 'LINK' in out
Example #16
0
    def process(self,
                user_slug,
                doc_slug,
                parts_dict,
                fragment=False,
                preview=False):
        """
        Generate a complete HTML document from a dictionary.

        - To process a directory, supply a dictionary with no 'index' key.
        - To process a document, supply a dictionary with an index entry.
        - To process an index _only_, supply {'index': text}.
        - To process a section, supply {slug: text} when slug != 'index'.
        - To process a fragment, without a headline, supply {slug: text} with
          fragment=true (used in DEMO blocks).
        """

        if len(parts_dict) == 0:
            return ValueError("Document is empty.")

        validate_document(parts_dict, fragment)
        parts, files = clean_document(parts_dict)

        # ------------------------------------------------------
        # Add placeholders for elements not processed by the wiki
        # -------------------------------------------------------

        self.demo = Demo()
        self.backslashes = Backslashes()
        self.entities = Entities()
        self.verbatim = Verbatim()

        # Demo is first: entities(self.backslashes(verbatim(demo)))
        parts = pipe(
            [self.entities, self.backslashes, self.verbatim, self.demo],
            'insert', parts)

        self.settings.extract(parts)

        # @todo:decide on file support.
        self.settings.set_config('files', files)

        # @[Cross Reference]
        self.outline = Outline(parts, default_counters())
        self.cross_references = CrossReferences(parts, self.outline)

        # ^[marker]
        # ^ Reference
        self.footnotes = Footnotes(parts, self.outline, self.id_prefix)
        self.links = Links(self.footnotes, self.id_prefix)

        # #[Topic, sub-topic]
        # self.index = Index(self.outline)
        # self.tags = Tags(self.index)

        # ~[Author 2000, p.34]
        self.bibliography = Bibliography(parts, self.outline, self.id_prefix)
        self.citations = Citations(self.bibliography)

        parts = pipe(
            [
                self.cross_references,  # <-- call 'insert(parts)'
                self.links,
                # self.tags,
                self.citations
            ],
            'insert',
            parts)

        # -------------
        # Generate HTML
        # -------------

        html_parts = {}

        # if len(parts) == 1 or fragment:
        # number = 1
        # for slug in sorted(parts):
        # section = self.make_section(
        # [str(number)], slug, parts[slug], fragment, preview
        # )
        # number = + 1
        # html_parts[slug] = section
        # else:
        if 'index' in parts:
            _, title, _, summary = get_title_data(parts['index'], 'index')
            self.settings.set('TITLE', title)
            self.settings.set('SUMMARY', summary)
            html_parts['index'] = self.make_index(parts['index'])
            html_parts['index'] += web_buttons(user_slug, doc_slug)
            if not self.outline.single_page():
                edit_base_uri = self.settings.get_base_uri('edit')
                html_parts['index'] += self.outline.html(edit_base_uri)
                html_parts['index'] += self.outline.html_spare_parts(
                    parts, edit_base_uri)
        else:
            self.settings.set('TITLE', '')

        for (numbering, slug, _, _, _) in self.outline:
            if slug in parts and slug not in ['index', 'biblio']:
                # print u"MAKE SECTION"
                section = self.make_section(numbering, slug, parts[slug],
                                            fragment, preview)
                html_parts[slug] = section

        # -------------------------------------
        # Replace placeholder content (as HTML)
        # -------------------------------------

        html_parts = pipe(
            [
                self.cross_references,
                # self.tags,
                self.links,
                self.citations
            ],
            'replace',
            html_parts)

        html_parts = pipe(
            [self.demo, self.verbatim, self.backslashes, self.entities],
            'replace', html_parts)

        footnote_parts = self.footnotes.html_parts()
        footnote_parts = pipe([self.verbatim, self.backslashes, self.entities],
                              'replace', footnote_parts)

        html = '<article>\n'
        html += html_parts['index'] if 'index' in html_parts else ''

        for (numbering, slug, _, _, _) in self.outline:
            if slug in parts and slug not in ['index', 'biblio']:
                html += html_parts[slug]
                footnote_html = footnote_parts.get(slug)
                if footnote_html:
                    html += "<footer>"
                    html += "<hr class=\"div-left div-solid\" />"
                    html += footnote_html
                    html += "</footer>"

        html += self.make_footer()

        html += '</article>'

        return html