Repository: pulldown-cmark/pulldown-cmark (Rust)

Issue:
Table column alignments are not properly applied to headings
When rendering to HTML, the `align` attribute is put on the wrong `th` tag in some tables.

Minimal reproducer:
```
|  Title A  |  Title B  |  Title C  |
| --------- | --------- | --------- |
| Content   | Content   | Content   |

|  Title A  |  Title B  |  Title C  |  Title D  |
| --------- | --------- | --------- | ---------:|
| Content   | Content   | Conent    | Content   |

|  Title A  |  Title B  |
| --------- | --------- |
| Content   | Content   |

|  Title A  |  Title B  |  Title C  |  Title D  |
| --------- | --------- | --------- | ---------:|
| Content   | Content   | Conent    | Content   |
```

Produces the output:
```
<table><thead><tr><th>  Title A  </th><th>  Title B  </th><th>  Title C  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td><td> Content   </td></tr>
</tbody></table>
<table><thead><tr><th align="right">  Title A  </th><th>  Title B  </th><th>  Title C  </th><th>  Title D  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td><td> Conent    </td><td align="right"> Content   </td></tr>
</tbody></table>
<table><thead><tr><th>  Title A  </th><th>  Title B  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td></tr>
</tbody></table>
<table><thead><tr><th>  Title A  </th><th align="right">  Title B  </th><th>  Title C  </th><th>  Title D  </th></tr></thead><tbody>
<tr><td> Content   </td><td> Content   </td><td> Conent    </td><td align="right"> Content   </td></tr>
</tbody></table>
```

Note that the first or second column is right-aligned, when the fourth column should be.

Proposed patch that fixes the immediate issue:
```
@@ -94,6 +94,7 @@ impl<'a, 'b, I: Iterator<Item=Event<'a>>> Ctx<'b, I> {
                 self.buf.push_str("<table>");
             }
             Tag::TableHead => {
+                self.table_cell_index = 0;
                 self.table_state = TableState::Head;
                 self.buf.push_str("<thead><tr>");
             }
```

If headings should not follow the column alignment (to match GitHub-flavored-markdown behavior):
```
@@ -104,13 +105,15 @@ impl<'a, 'b, I: Iterator<Item=Event<'a>>> Ctx<'b, I> {
             Tag::TableCell => {
                 match self.table_state {
                     TableState::Head => self.buf.push_str("<th"),
-                    TableState::Body => self.buf.push_str("<td"),
-                }
-                match self.table_alignments.get(self.table_cell_index) {
-                    Some(&Alignment::Left) => self.buf.push_str(" align=\"left\""),
-                    Some(&Alignment::Center) => self.buf.push_str(" align=\"center\""),
-                    Some(&Alignment::Right) => self.buf.push_str(" align=\"right\""),
-                    _ => (),
+                    TableState::Body => {
+                        self.buf.push_str("<td");
+                        match self.table_alignments.get(self.table_cell_index) {
+                            Some(&Alignment::Left) => self.buf.push_str(" align=\"left\""),
+                            Some(&Alignment::Center) => self.buf.push_str(" align=\"center\""),
+                            Some(&Alignment::Right) => self.buf.push_str(" align=\"right\""),
+                            _ => (),
+                        }
+                    }
                 }
                 self.buf.push_str(">");
             }
```


Discussion:
Can still reproduce this too. Investigating..
It seems that GFM does stipulate that [heading should follow column alignment](https://github.github.com/gfm/#example-192), at least in the current version.

Your first one-liner should do the trick. I'm opening an PR with that fix :-)



Patch:
diff --git a/specs/regression.txt b/specs/regression.txt
new file mode 100644
index 00000000..04ed834b
--- /dev/null
+++ b/specs/regression.txt
@@ -0,0 +1,22 @@
+Run this with `cargo run -- -T -F -s specs/regression.txt`.
+
+Regression tests for issues on the github issue tracker.
+
+ISSUE #145
+
+```````````````````````````````` example
+|  Title A  |  Title B  |
+| --------- | --------- |
+| Content   | Content   |
+
+|  Title A  |  Title B  |  Title C  |  Title D  |
+| --------- | --------- | --------- | ---------:|
+| Content   | Content   | Conent    | Content   |
+.
+<table><thead><tr><th>Title A  </th><th>Title B  </th></tr></thead><tbody>
+<tr><td>Content   </td><td>Content   </td></tr>
+</tbody></table>
+<table><thead><tr><th>Title A  </th><th>Title B  </th><th>Title C  </th><th align="right">Title D  </th></tr></thead><tbody>
+<tr><td>Content   </td><td>Content   </td><td>Conent    </td><td align="right">Content   </td></tr>
+</tbody></table>
+````````````````````````````````
diff --git a/src/html.rs b/src/html.rs
index 0b1973af..e61e5160 100644
--- a/src/html.rs
+++ b/src/html.rs
@@ -146,6 +146,7 @@ where
             }
             Tag::TableHead => {
                 self.table_state = TableState::Head;
+                self.table_cell_index = 0;
                 self.write(b"<thead><tr>", false)
             }
             Tag::TableRow => {


Test patch:
diff --git a/tests/regression.rs b/tests/regression.rs
new file mode 100644
index 00000000..27218147
--- /dev/null
+++ b/tests/regression.rs
@@ -0,0 +1,39 @@
+// This file is auto-generated by the build script
+// Please, do not modify it manually
+
+extern crate pulldown_cmark;
+
+include!("normalize_html.rs.inc");
+
+
+    #[test]
+    fn regression_test_1() {
+        let original = r##"|  Title A  |  Title B  |
+| --------- | --------- |
+| Content   | Content   |
+
+|  Title A  |  Title B  |  Title C  |  Title D  |
+| --------- | --------- | --------- | ---------:|
+| Content   | Content   | Conent    | Content   |
+"##;
+        let expected = r##"<table><thead><tr><th>Title A  </th><th>Title B  </th></tr></thead><tbody>
+<tr><td>Content   </td><td>Content   </td></tr>
+</tbody></table>
+<table><thead><tr><th>Title A  </th><th>Title B  </th><th>Title C  </th><th align="right">Title D  </th></tr></thead><tbody>
+<tr><td>Content   </td><td>Content   </td><td>Conent    </td><td align="right">Content   </td></tr>
+</tbody></table>
+"##;
+
+        use pulldown_cmark::{Parser, html, Options};
+
+        let mut s = String::new();
+
+        let mut opts = Options::empty();
+        opts.insert(Options::ENABLE_TABLES);
+        opts.insert(Options::ENABLE_FOOTNOTES);
+
+        let p = Parser::new_ext(&original, opts);
+        html::push_html(&mut s, p);
+
+        assert_eq!(normalize_html(&expected), normalize_html(&s));
+    }
\ No newline at end of file