Skip to content

Commit

Permalink
Added th to table headers so that styling with things like Twitter Bo…
Browse files Browse the repository at this point in the history
…otstrap and typeset.css work as expected. Cells in headers should always be TH unless they are advisory cells within headers in which case TD is acceptable (but being Markdown a user with such needs could just enter HTML for this)
  • Loading branch information
David Kitchen committed Oct 16, 2013
1 parent 48d1f9d commit 6e6572e
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
19 changes: 14 additions & 5 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -806,12 +806,21 @@ func (p *parser) tableRow(out *bytes.Buffer, data []byte, columns []int) {

var cellWork bytes.Buffer
p.inline(&cellWork, data[cellStart:cellEnd])
p.r.TableCell(&rowWork, cellWork.Bytes(), columns[col])

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++ {
p.r.TableCell(&rowWork, nil, 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
Expand Down
17 changes: 17 additions & 0 deletions html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions latex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(" & ")
Expand Down
1 change: 1 addition & 0 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 6e6572e

Please sign in to comment.