Codebase list golang-github-jaytaylor-html2text / a93a6c6
feat(tables): add tablewriter settings passthrough Frank Spitulski authored 5 years ago J. Elliot Taylor committed 5 years ago
2 changed file(s) with 66 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
1414
1515 // Options provide toggles and overrides to control specific rendering behaviors.
1616 type Options struct {
17 PrettyTables bool // Turns on pretty ASCII rendering for table elements.
18 OmitLinks bool // Turns on omitting links
17 PrettyTables bool // Turns on pretty ASCII rendering for table elements.
18 PrettyTablesOptions *PrettyTablesOptions // Configures pretty ASCII rendering for table elements.
19 OmitLinks bool // Turns on omitting links
20 }
21
22 // PrettyTablesOptions overrides tablewriter behaviors
23 type PrettyTablesOptions struct {
24 AutoFormatHeader bool
25 AutoWrapText bool
26 ReflowDuringAutoWrap bool
27 ColWidth int
28 ColumnSeparator string
29 RowSeparator string
30 CenterSeparator string
31 HeaderAlignment int
32 FooterAlignment int
33 Alignment int
34 ColumnAlignment []int
35 NewLine string
36 HeaderLine bool
37 RowLine bool
38 AutoMergeCells bool
39 Borders tablewriter.Border
40 }
41
42 // NewPrettyTablesOptions creates PrettyTablesOptions with default settings
43 func NewPrettyTablesOptions() *PrettyTablesOptions {
44 return &PrettyTablesOptions{
45 AutoFormatHeader: true,
46 AutoWrapText: true,
47 ReflowDuringAutoWrap: true,
48 ColWidth: tablewriter.MAX_ROW_WIDTH,
49 ColumnSeparator: tablewriter.COLUMN,
50 RowSeparator: tablewriter.ROW,
51 CenterSeparator: tablewriter.CENTER,
52 HeaderAlignment: tablewriter.ALIGN_DEFAULT,
53 FooterAlignment: tablewriter.ALIGN_DEFAULT,
54 Alignment: tablewriter.ALIGN_DEFAULT,
55 ColumnAlignment: []int{},
56 NewLine: tablewriter.NEWLINE,
57 HeaderLine: true,
58 RowLine: false,
59 AutoMergeCells: false,
60 Borders: tablewriter.Border{Left: true, Right: true, Bottom: true, Top: true},
61 }
1962 }
2063
2164 // FromHTMLNode renders text output from a pre-parsed HTML document.
276319
277320 buf := &bytes.Buffer{}
278321 table := tablewriter.NewWriter(buf)
322 if ctx.options.PrettyTablesOptions != nil {
323 options := ctx.options.PrettyTablesOptions
324 table.SetAutoFormatHeaders(options.AutoFormatHeader)
325 table.SetAutoWrapText(options.AutoWrapText)
326 table.SetReflowDuringAutoWrap(options.ReflowDuringAutoWrap)
327 table.SetColWidth(options.ColWidth)
328 table.SetColumnSeparator(options.ColumnSeparator)
329 table.SetRowSeparator(options.RowSeparator)
330 table.SetCenterSeparator(options.CenterSeparator)
331 table.SetHeaderAlignment(options.HeaderAlignment)
332 table.SetFooterAlignment(options.FooterAlignment)
333 table.SetAlignment(options.Alignment)
334 table.SetColumnAlignment(options.ColumnAlignment)
335 table.SetNewLine(options.NewLine)
336 table.SetHeaderLine(options.HeaderLine)
337 table.SetRowLine(options.RowLine)
338 table.SetAutoMergeCells(options.AutoMergeCells)
339 table.SetBorders(options.Borders)
340 }
279341 table.SetHeader(ctx.tableCtx.header)
280342 table.SetFooter(ctx.tableCtx.footer)
281343 table.AppendBulk(ctx.tableCtx.body)
333333
334334 for _, testCase := range testCases {
335335 options := Options{
336 PrettyTables: true,
336 PrettyTables: true,
337 PrettyTablesOptions: NewPrettyTablesOptions(),
337338 }
338339 // Check pretty tabular ASCII version.
339340 if msg, err := wantString(testCase.input, testCase.tabularOutput, options); err != nil {