packagetable2rstimport("golang.org/x/net/html""strings")typeElementTypeintconst(TextNodeElementType=iotatableElementNodetbodyElementNodetrElementNodetdElementNodeNoNeedToKnow)funcgetElementType(n*html.Node)ElementType{ifn.Type==html.TextNode{returnTextNode}ifn.Type==html.ElementNode&&n.Data=="td"{returntdElementNode}ifn.Type==html.ElementNode&&n.Data=="tr"{returntrElementNode}ifn.Type==html.ElementNode&&n.Data=="tbody"{returntbodyElementNode}ifn.Type==html.ElementNode&&n.Data=="table"{returntableElementNode}returnNoNeedToKnow}funcgetTextNodeRst(text*html.Node)string{returnstrings.TrimSpace(text.Data)}funcgetTdRst(td*html.Node)string{s:=""forc:=td.FirstChild;c!=nil;c=c.NextSibling{ifgetElementType(c)==TextNode{s+=(getTextNodeRst(c)+"\n")continue}panic("cannot convert this td")}returns}funcgetTrRst(tr*html.Node)string{s:=""isFirstTd:=trueforc:=tr.FirstChild;c!=nil;c=c.NextSibling{ifgetElementType(c)==tdElementNode{ifisFirstTd{s+=(" * - "+getTdRst(c))isFirstTd=false}else{s+=(" - "+getTdRst(c))}continue}ifgetElementType(c)==TextNode{s+=getTextNodeRst(c)continue}panic("cannot convert this tr")}returns}funcgetTbodyRst(tbody*html.Node)string{s:=""forc:=tbody.FirstChild;c!=nil;c=c.NextSibling{ifgetElementType(c)==trElementNode{s+=getTrRst(c)continue}ifgetElementType(c)==TextNode{s+=getTextNodeRst(c)continue}panic("cannot convert this tbody")}returns}funcgetTableRst(table*html.Node)string{s:=".. list-table::\n\n"forc:=table.FirstChild;c!=nil;c=c.NextSibling{ifgetElementType(c)==tbodyElementNode{s+=getTbodyRst(c)continue}ifgetElementType(c)==TextNode{s+=getTextNodeRst(c)continue}panic("cannot convert this table")}returns}functraverse(n*html.Node)string{s:=""forc:=n.FirstChild;c!=nil;c=c.NextSibling{ifgetElementType(c)==tableElementNode{s+=getTableRst(c)continue}else{s+=traverse(c)}}returns}funcHtmlTableToRstListTable(doc*html.Node)string{returntraverse(doc)}