Merge pull request #43 from microcosm-cc/master

Cells in THEAD > TR are now TH.
This commit is contained in:
Vytautas Šaltenis 2014-01-08 11:46:30 -08:00
commit 0c38d23ca2
5 changed files with 49 additions and 15 deletions

View File

@ -654,7 +654,7 @@ func (p *parser) table(out *bytes.Buffer, data []byte) int {
// include the newline in data sent to tableRow
i++
p.tableRow(&body, data[rowStart:i], columns)
p.tableRow(&body, data[rowStart:i], columns, false)
}
p.r.Table(out, header.Bytes(), body.Bytes(), columns)
@ -771,12 +771,12 @@ func (p *parser) tableHeader(out *bytes.Buffer, data []byte) (size int, columns
return
}
p.tableRow(out, header, columns)
p.tableRow(out, header, columns, true)
size = i + 1
return
}
func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int, header bool) {
i, col := 0, 0
var rowWork bytes.Buffer
@ -806,13 +806,22 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {
var cellWork bytes.Buffer
p.inline(&cellWork, data[cellStart:cellEnd])
if header {
p.r.TableHeaderCell(&rowWork, cellWork.Bytes(), columns[col])
} else {
p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])
}
}
// pad it out with empty columns to get the right number
for ; col < len(columns); col++ {
if header {
p.r.TableHeaderCell(&rowWork, nil, columns[col])
} else {
p.r.TableCell(&rowWork, nil, columns[col])
}
}
// silently ignore rows with too many cells

View File

@ -649,46 +649,46 @@ func TestFencedCodeBlock(t *testing.T) {
func TestTable(t *testing.T) {
var tests = []string{
"a | b\n---|---\nc | d\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td>c</td>\n<td>d</td>\n</tr>\n</tbody>\n</table>\n",
"a | b\n---|--\nc | d\n",
"<p>a | b\n---|--\nc | d</p>\n",
"|a|b|c|d|\n|----|----|----|---|\n|e|f|g|h|\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n<td>d</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n<th>d</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td>e</td>\n<td>f</td>\n<td>g</td>\n<td>h</td>\n</tr>\n</tbody>\n</table>\n",
"*a*|__b__|[c](C)|d\n---|---|---|---\ne|f|g|h\n",
"<table>\n<thead>\n<tr>\n<td><em>a</em></td>\n<td><strong>b</strong></td>\n<td><a href=\"C\">c</a></td>\n<td>d</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th><em>a</em></th>\n<th><strong>b</strong></th>\n<th><a href=\"C\">c</a></th>\n<th>d</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td>e</td>\n<td>f</td>\n<td>g</td>\n<td>h</td>\n</tr>\n</tbody>\n</table>\n",
"a|b|c\n---|---|---\nd|e|f\ng|h\ni|j|k|l|m\nn|o|p\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td>d</td>\n<td>e</td>\n<td>f</td>\n</tr>\n\n" +
"<tr>\n<td>g</td>\n<td>h</td>\n<td></td>\n</tr>\n\n" +
"<tr>\n<td>i</td>\n<td>j</td>\n<td>k</td>\n</tr>\n\n" +
"<tr>\n<td>n</td>\n<td>o</td>\n<td>p</td>\n</tr>\n</tbody>\n</table>\n",
"a|b|c\n---|---|---\n*d*|__e__|f\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td><em>d</em></td>\n<td><strong>e</strong></td>\n<td>f</td>\n</tr>\n</tbody>\n</table>\n",
"a|b|c|d\n:--|--:|:-:|---\ne|f|g|h\n",
"<table>\n<thead>\n<tr>\n<td align=\"left\">a</td>\n<td align=\"right\">b</td>\n" +
"<td align=\"center\">c</td>\n<td>d</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th align=\"left\">a</th>\n<th align=\"right\">b</th>\n" +
"<th align=\"center\">c</th>\n<th>d</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td align=\"left\">e</td>\n<td align=\"right\">f</td>\n" +
"<td align=\"center\">g</td>\n<td>h</td>\n</tr>\n</tbody>\n</table>\n",
"a|b|c\n---|---|---\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n</tr>\n</thead>\n\n<tbody>\n</tbody>\n</table>\n",
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n</tr>\n</thead>\n\n<tbody>\n</tbody>\n</table>\n",
"a| b|c | d | e\n---|---|---|---|---\nf| g|h | i |j\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b</td>\n<td>c</td>\n<td>d</td>\n<td>e</td>\n</tr>\n</thead>\n\n" +
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b</th>\n<th>c</th>\n<th>d</th>\n<th>e</th>\n</tr>\n</thead>\n\n" +
"<tbody>\n<tr>\n<td>f</td>\n<td>g</td>\n<td>h</td>\n<td>i</td>\n<td>j</td>\n</tr>\n</tbody>\n</table>\n",
"a|b\\|c|d\n---|---|---\nf|g\\|h|i\n",
"<table>\n<thead>\n<tr>\n<td>a</td>\n<td>b|c</td>\n<td>d</td>\n</tr>\n</thead>\n\n<tbody>\n<tr>\n<td>f</td>\n<td>g|h</td>\n<td>i</td>\n</tr>\n</tbody>\n</table>\n",
"<table>\n<thead>\n<tr>\n<th>a</th>\n<th>b|c</th>\n<th>d</th>\n</tr>\n</thead>\n\n<tbody>\n<tr>\n<td>f</td>\n<td>g|h</td>\n<td>i</td>\n</tr>\n</tbody>\n</table>\n",
}
doTestsBlock(t, tests, EXTENSION_TABLES)
}

17
html.go
View File

@ -305,6 +305,23 @@ func (options *Html) TableRow(out *bytes.Buffer, text []byte) {
out.WriteString("\n</tr>\n")
}
func (options *Html) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
doubleSpace(out)
switch align {
case TABLE_ALIGNMENT_LEFT:
out.WriteString("<th align=\"left\">")
case TABLE_ALIGNMENT_RIGHT:
out.WriteString("<th align=\"right\">")
case TABLE_ALIGNMENT_CENTER:
out.WriteString("<th align=\"center\">")
default:
out.WriteString("<th>")
}
out.Write(text)
out.WriteString("</th>")
}
func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
doubleSpace(out)
switch align {

View File

@ -151,6 +151,13 @@ func (options *Latex) TableRow(out *bytes.Buffer, text []byte) {
out.Write(text)
}
func (options *Latex) TableHeaderCell(out *bytes.Buffer, text []byte, align int) {
if out.Len() > 0 {
out.WriteString(" & ")
}
out.Write(text)
}
func (options *Latex) TableCell(out *bytes.Buffer, text []byte, align int) {
if out.Len() > 0 {
out.WriteString(" & ")

View File

@ -140,6 +140,7 @@ type Renderer interface {
Paragraph(out *bytes.Buffer, text func() bool)
Table(out *bytes.Buffer, header []byte, body []byte, columnData []int)
TableRow(out *bytes.Buffer, text []byte)
TableHeaderCell(out *bytes.Buffer, text []byte, flags int)
TableCell(out *bytes.Buffer, text []byte, flags int)
Footnotes(out *bytes.Buffer, text func() bool)
FootnoteItem(out *bytes.Buffer, name, text []byte, flags int)