Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/html2markdown/cmd/cmd_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (cli *CLI) convert(input []byte) ([]byte, error) {
table.WithSpanCellBehavior(table.SpanCellBehavior(cli.config.tableSpanCellBehavior)),
table.WithPresentationTables(cli.config.tablePresentationTables),
table.WithNewlineBehavior(table.NewlineBehavior(cli.config.tableNewlineBehavior)),
table.WithCellPadding(table.CellPaddingBehavior(cli.config.tableCellPaddingBehavior)),
),
)
}
Expand Down
13 changes: 7 additions & 6 deletions cli/html2markdown/cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ type Config struct {
// - - - - - Plugins - - - - - //
enablePluginStrikethrough bool

enablePluginTable bool
tableSkipEmptyRows bool
tableHeaderPromotion bool
tableSpanCellBehavior string
tablePresentationTables bool
tableNewlineBehavior string
enablePluginTable bool
tableSkipEmptyRows bool
tableHeaderPromotion bool
tableSpanCellBehavior string
tablePresentationTables bool
tableNewlineBehavior string
tableCellPaddingBehavior string
}

// Release holds the information (from the 3 ldflags) that goreleaser sets.
Expand Down
4 changes: 4 additions & 0 deletions cli/html2markdown/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func (cli *CLI) initFlags(progname string) {
cli.flags.StringVar(&cli.config.tableSpanCellBehavior, "opt-table-span-cell-behavior", "", `[for --plugin-table] how colspan/rowspan should be rendered: "empty" or "mirror"`)
cli.flags.BoolVar(&cli.config.tablePresentationTables, "opt-table-presentation-tables", false, `[for --plugin-table] whether tables with role="presentation" should be converted`)
cli.flags.StringVar(&cli.config.tableNewlineBehavior, "opt-table-newline-behavior", "", `[for --plugin-table] how tables containing newlines should be handled: "skip" or "preserve"`)
cli.flags.StringVar(&cli.config.tableCellPaddingBehavior, "opt-table-cell-padding", "", `[for --plugin-table] whether cells in the tables should include extra padding for visual continuity: "aligned", "minimal", or "none"`)
}

func (cli *CLI) parseFlags(args []string) error {
Expand All @@ -120,6 +121,9 @@ func (cli *CLI) parseFlags(args []string) error {
if cli.config.tableNewlineBehavior != "" && !cli.config.enablePluginTable {
return fmt.Errorf("--opt-table-newline-behavior requires --plugin-table to be enabled")
}
if cli.config.tableCellPaddingBehavior != "" && !cli.config.enablePluginTable {
return fmt.Errorf("--opt-table-cell-padding requires --plugin-table to be enabled")
}

// TODO: use constant for flag name & use formatFlag
// var keyStrongDelimiter = "opt-strong-delimiter"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Use a HTML sanitizer before displaying the HTML in the browser!
Make bold text. Should <strong> be indicated by two asterisks or two underscores?
"**" or "__" (default: "**")

--opt-table-cell-padding
[for --plugin-table] whether cells in the tables should include extra padding for visual continuity: "aligned", "minimal", or "none"

--opt-table-header-promotion
[for --plugin-table] first row should be treated as a header

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ Use a HTML sanitizer before displaying the HTML in the browser!
Make bold text. Should <strong> be indicated by two asterisks or two underscores?
"**" or "__" (default: "**")

--opt-table-cell-padding
[for --plugin-table] whether cells in the tables should include extra padding for visual continuity: "aligned", "minimal", or "none"

--opt-table-header-promotion
[for --plugin-table] first row should be treated as a header

Expand Down
22 changes: 17 additions & 5 deletions plugin/table/3_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func (s *tablePlugin) writeHeaderUnderline(w converter.Writer, alignments []stri
w.WriteString("-")
}

w.WriteString(strings.Repeat("-", maxLength))
if s.cellPaddingBehavior == CellPaddingAligned {
w.WriteString(strings.Repeat("-", maxLength))
} else {
w.WriteString("-")
}

if align == "right" || align == "center" {
w.WriteString(":")
Expand All @@ -90,16 +94,24 @@ func (s *tablePlugin) writeRow(w converter.Writer, counts []int, cells [][]byte)
if isFirstCell {
w.WriteString("|")
}
w.WriteString(" ")
w.Write(cell)

currentCount := utf8.RuneCount(cell)
filler := counts[i] - currentCount

if filler > 0 {
if s.cellPaddingBehavior == CellPaddingAligned || s.cellPaddingBehavior == CellPaddingMinimal {
w.WriteString(" ")
}

w.Write(cell)

if s.cellPaddingBehavior == CellPaddingAligned && filler > 0 {
w.WriteString(strings.Repeat(" ", filler))
}

w.WriteString(" |")
if s.cellPaddingBehavior == CellPaddingAligned || s.cellPaddingBehavior == CellPaddingMinimal {
w.WriteString(" ")
}

w.WriteString("|")
}
}
34 changes: 34 additions & 0 deletions plugin/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,39 @@ func WithNewlineBehavior(behavior NewlineBehavior) option {
}
}

type CellPaddingBehavior string

const (
// CellPaddingAligned adds visual padding to cells to make each column equal width (default).
CellPaddingAligned CellPaddingBehavior = "aligned"
// CellPaddingMinimal keeps a very small amount of padding to balance table readability while also reducing character count.
CellPaddingMinimal CellPaddingBehavior = "minimal"
// CellPaddingNone refrains from adding the padding to the cells.
CellPaddingNone CellPaddingBehavior = "none"
)

// WithPadColumns configures how to handle padding in table cells.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to WithCellPadding

// When set to "aligned" (default), every cell's text is padded to the width of the largest cell in its column.
// When set to "minimal", every cell gets a space at the beginning and end of the cell for some minimal padding.
// When set to "none", no extra padding is applied to cells.
func WithCellPadding(behavior CellPaddingBehavior) option {
return func(p *tablePlugin) error {
switch behavior {
case "":
// Allow empty string to default to "aligned"
p.cellPaddingBehavior = CellPaddingBehavior(CellPaddingAligned)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wrapping with CellPaddingBehavior() is not needed here since it has already the correct type.

return nil

case CellPaddingAligned, CellPaddingMinimal, CellPaddingNone:
p.cellPaddingBehavior = behavior
return nil

default:
return fmt.Errorf("unknown value %q for pad columns behavior", behavior)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update naming

}
}
}

// WithSkipEmptyRows configures the table plugin to omit empty rows from the output.
// An empty row is defined as a row where all cells contain no content or only whitespace.
// When set to true, empty rows will be omitted from the output. When false (default),
Expand Down Expand Up @@ -113,6 +146,7 @@ type tablePlugin struct {
skipEmptyRows bool
promoteFirstRowToHeader bool
convertPresentationTables bool
cellPaddingBehavior CellPaddingBehavior
}

func (p *tablePlugin) setError(err error) {
Expand Down
Loading