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

Last cell of table missing if blank #118

Closed
JohnPickerill opened this issue Sep 12, 2016 · 8 comments
Closed

Last cell of table missing if blank #118

JohnPickerill opened this issue Sep 12, 2016 · 8 comments

Comments

@JohnPickerill
Copy link

If the last table of a cell is blank then it is missing in the rendered HTML.
This appears to be because the last pipe is removed to avoid an extra row being created if by the split function.
A crude fix to this would look something like

def parse_table(self, m):
    item = self._process_table(m)
    #JP fix for blank last cell in table
    rmat = re.match(r'^(.*)(?<=\|)(.*)\|\s*\n$',m.group(3),re.S)
    if  rmat:
        if rmat.group(2).strip() == '':
            cells = rmat.group(1) +'|'
        else:               
            cells = rmat.group(1) + rmat.group(2) 
    # original
    #cells = re.sub(r'(?: *\| *)?\n$', '', m.group(3))
    logger_m.debug(cells)

    cells = cells.split('\n')
    for i, v in enumerate(cells):
        v = re.sub(r'^ *\| *| *\| *$', '', v)
        cells[i] = re.split(r' *\| *', v)

    item['cells'] = cells
    self.tokens.append(item) 
@JohnPickerill
Copy link
Author

The above fix doesn't cover the case where the table is the very last thing in the file this needs the re to be changed to
rmat = re.match(r'^(.)(?<=|)(.)|\s*\n?$',m.group(3),re.S)

@JohnPickerill
Copy link
Author

s

@JohnPickerill JohnPickerill reopened this Sep 23, 2016
@lepture
Copy link
Owner

lepture commented Sep 25, 2016

Could you add a test case, and send the patch as a pull request?

@JohnPickerill
Copy link
Author

OK will do

On 25 September 2016 at 04:42, Hsiaoming Yang [email protected]
wrote:

Could you add a test case, and send the patch as a pull request?


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
#118 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AELX8rOzdKr2m6Dy9AKaIuY_wBVWAP5lks5qte2wgaJpZM4J6rHp
.

@m-holger
Copy link

A simple fix for the problem would be

    def parse_table(self, m):
        item = self._process_table(m)
        cols = len(item['header'])  #added
        cells = re.sub(r'(?: *\| *)?\n$', '', m.group(3))
        cells = cells.split('\n')
        for i, v in enumerate(cells):
            v = re.sub(r'^ *\| *| *\| *$', '', v)
            cells[i] = re.split(r' *(?<!\\)\| *', v)
            #
            # The header row must match the delimiter row in the number of cells. 
            # If not, a table will not be recognized. The remainder of the table’s 
            # rows may vary in the number of cells. If there are a number of cells 
            # fewer than the number of cells in the header row, empty cells are 
            # inserted. See https://github.github.com/gfm/#example-203
            while len(cells[i]) < cols: #added
                cells[i].append('')
            # If there are greater, the excess is ignored
            # see https://github.github.com/gfm/#example-203    
            del cells[i][cols:]  #added

        item['cells'] = self._process_cells(cells)
        self.tokens.append(item)

@lepture
Copy link
Owner

lepture commented Dec 7, 2019

please try v2.0.0a1

@lepture lepture closed this as completed Dec 7, 2019
@lifton
Copy link

lifton commented Apr 25, 2022

This issue appears to still be a problem in v2.0.2. Here's a test case:

import mistune

s = '''
| Foo | Bar |
|-----|-----|
| boo | baz |
| faz |     |
'''

print(mistune.html(s))

which yields the following output:

<table>
<thead>
<tr>
  <th>Foo</th>
  <th>Bar</th>
</tr>
</thead>
<tbody>
<tr>
  <td>boo</td>
  <td>baz</td>
</tr>
<tr>
  <td>faz</td>
</tr>
</tbody>
</table>

The expected output is:

<table>
<thead>
<tr>
  <th>Foo</th>
  <th>Bar</th>
</tr>
</thead>
<tbody>
<tr>
  <td>boo</td>
  <td>baz</td>
</tr>
<tr>
  <td>faz</td>
  <td></td>
</tr>
</tbody>
</table>

Please re-open this issue.

@lepture lepture reopened this Apr 25, 2022
lepture added a commit that referenced this issue Apr 25, 2022
@lepture
Copy link
Owner

lepture commented Apr 25, 2022

@lifton just fixed in master code.

@lepture lepture closed this as completed Jul 14, 2022
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

No branches or pull requests

4 participants