File: test_md_table.py

package info (click to toggle)
ford 7.0.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,004 kB
  • sloc: python: 12,017; f90: 419; javascript: 51; fortran: 45; makefile: 23
file content (78 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download
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
from ford.md_striped_table import StripedTableCSSExtension
import markdown
from bs4 import BeautifulSoup
import textwrap


def test_zebra_table():
    """Check that alternate rows have different classes"""
    md = markdown.Markdown(
        extensions=["markdown.extensions.extra", StripedTableCSSExtension()],
        output_format="html5",
    )

    text = textwrap.dedent("""
        | Header1    | Header2     |
        | ---------- | ----------- |
        | some text  | some text   |
        | some text  | some text   |
        | some text  | some text   |
        | some text  | some text   |
        """)

    html = md.convert(text)
    soup = BeautifulSoup(html, features="html.parser")

    print(soup)
    assert "table" in soup.table.attrs["class"]
    assert "table-striped" in soup.table.attrs["class"]


def test_no_clobber_html_table():
    """Check that tables with CSS already applied don't get modified"""
    md = markdown.Markdown(
        extensions=["markdown.extensions.extra", StripedTableCSSExtension()],
        output_format="html5",
    )

    text = textwrap.dedent("""
        <table class="dont-touch">
        <thead>
        <tr>
        <th>Header1</th>
        <th>Header2</th>
        </tr>
        </thead>
        <tbody>
        <tr class="row1">
        <td>some text</td>
        <td>some text</td>
        </tr>
        <tr class="row2">
        <td>some text</td>
        <td>some text</td>
        </tr>
        <tr class="row3">
        <td>some text</td>
        <td>some text</td>
        </tr>
        <tr class="row4">
        <td>some text</td>
        <td>some text</td>
        </tr>
        </tbody>
        </table>
        """)

    html = md.convert(text)
    soup = BeautifulSoup(html, features="html.parser")

    print(soup)
    assert soup.table.attrs["class"] == ["dont-touch"]

    body = soup.table.find("tbody")
    rows = body.find_all("tr")
    assert rows[0].attrs["class"] == ["row1"]
    assert rows[1].attrs["class"] == ["row2"]
    assert rows[2].attrs["class"] == ["row3"]
    assert rows[3].attrs["class"] == ["row4"]