Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for colspan in tables #10

Open
hughdavenport opened this issue Aug 3, 2021 · 2 comments · May be fixed by #11
Open

Support for colspan in tables #10

hughdavenport opened this issue Aug 3, 2021 · 2 comments · May be fixed by #11

Comments

@hughdavenport
Copy link
Contributor

For a project I'm working on, having colspan would be useful, like described at github/markup#1151

Basically would want something like this to be able to be generated. I've opened a support ticket with GFM to see whether it could be added to the spec.

<table>
    <tr><th colspan=2>Logical Operators</th></tr>
    <tr><td>&&</td><td>Logical and</td></tr>
    <tr><td>||</td><td>Logical or</td></tr>
    <tr><td>!</td><td>Logical not</td></tr>
    <tr><td>? :</td><td>Logical ternary</td></tr>
</table>

Perhaps the syntax could be something like (using |- as a separator that is spanning, which would then ignore that contents?):

| Logical Operators  |-|
|:---:| --- |
| && | Logical and      |
| \|\|  | Logical or         |
| !     | Logical not       |
| ? :  | Logical ternary |
hughdavenport added a commit to hughdavenport/CommonMark-py-Extensions that referenced this issue Aug 3, 2021
This adds support for having colspan generated in tables. The spec is yet to be updated with GFM.

A cell with the sole contents of `-` will be ignored, and previous cells in the row will have a colspan attribute.

Side effect would be that users can't have a cell with just `-` in it.

Fixes GovReady#10
@hughdavenport hughdavenport linked a pull request Aug 3, 2021 that will close this issue
@hughdavenport
Copy link
Contributor Author

I've made PR #11 for this.

@hughdavenport
Copy link
Contributor Author

If support is needed now, this is what I use:

def check_commonmark_tables_bug10():

    class RendererWaitingForBug10(commonmark_extensions.tables.RendererWithTables):
        def table(self, node, entering):
            if entering:
                self.lit(self.make_table_node(node) + "\n")
                for i, part in enumerate(node.table):
                    if i == 0:
                        part_tag = "thead"
                    else:
                        part_tag = "tbody"
                    self.lit("<" + part_tag + ">\n")
                    for row in part:
                        self.lit("<tr>\n")
                        for colidx, cell in enumerate(row):
                            if cell.first_child and cell.first_child.literal == '-':
                                continue
                            if part_tag == "thead":
                                col_tag = "th"
                                if self.options.get("table_th_scope"):
                                    col_attrs = '  scope="col"'
                                else:
                                    col_attrs = ''
                            else:
                                col_tag = "td"
                                col_attrs = ""

                            if colidx in node.column_properties and "align" in node.column_properties[colidx]:
                                col_attrs += ' align=\"' + node.column_properties[colidx]["align"] + "\""

                            span = 1
                            for nextid in range(colidx+1, len(row)):
                                if row[nextid].first_child and row[nextid].first_child.literal == '-':
                                    span += 1
                                else:
                                    break
                            if span > 1:
                                col_attrs += ' colspan=\"' + str(span) + '\"'

                            self.lit("<" + col_tag + col_attrs + ">")

                            import copy
                            inner_renderer = copy.copy(self)
                            cell = inner_renderer.render(cell)

                            # If the cell is just one <p>, unwrap it.
                            m = re.match("<p>(.*)</p>$", cell)
                            if m:
                                cell = m.group(1)

                            self.lit(cell)
                            self.lit("</" + col_tag + ">\n")
                        self.lit("</tr>\n")
                    self.lit("</" + part_tag + ">\n")
                self.lit("</table>\n")

    parser = commonmark_extensions.tables.ParserWithTables()
    renderer = commonmark_extensions.tables.RendererWithTables()
    example_with_cols = """| foo | -   |
| --- | --- |
| bar | baz |"""
    example_with_cols_expected = "".join("""<table>
<thead>
<tr>
<th colspan="2">foo</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
</tbody>
</table>""".split())

    if "".join(renderer.render(parser.parse(example_with_cols)).split()) != example_with_cols_expected:
        eprint("Missing update for commonmark_extensions (bug #10),",
                "applying monkeypatch")
        return RendererWaitingForBug10()
    return renderer

renderer = check_commonmark_tables_bug10()
# use renderer as normal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant