packageaulli2rstimport("bufio""fmt""golang.org/x/net/html""io""os""strings")funcStringToLines(sstring)[]string{varlines[]stringscanner:=bufio.NewScanner(strings.NewReader(s))forscanner.Scan(){lines=append(lines,scanner.Text())}iferr:=scanner.Err();err!=nil{fmt.Fprintln(os.Stderr,"reading standard input:",err)}returnlines}funcindentEachLine(sstring)string{lines:=StringToLines(s)varindentedLines[]stringfor_,line:=rangelines{indentedLines=append(indentedLines," "+line)}returnstrings.Join(indentedLines,"\n")}funcisAnchorElement(n*html.Node)bool{returnn.Type==html.ElementNode&&n.Data=="a"}funcisUlElement(n*html.Node)bool{returnn.Type==html.ElementNode&&n.Data=="ul"}funcisLiElement(n*html.Node)bool{returnn.Type==html.ElementNode&&n.Data=="li"}funcisTextNode(n*html.Node)bool{returnn.Type==html.TextNode}funcgetAttribute(n*html.Node,keystring)(string,bool){for_,attr:=rangen.Attr{ifattr.Key==key{returnattr.Val,true}}return"",false}functextNode2rst(n*html.Node)string{text:=strings.TrimSpace(n.Data)iftext==""{return"\n"}returnn.Data}funca2rst(n*html.Node)string{text:=strings.TrimSpace(n.FirstChild.Data)href,ok:=getAttribute(n,"href")ifok{return"`"+text+" <"+href+">`__"}return""}funcli2rst(n*html.Node)string{rstText:=""forc:=n.FirstChild;c!=nil;c=c.NextSibling{ifisTextNode(c){rstText+=textNode2rst(c)}ifisAnchorElement(c){rstText+=a2rst(c)}ifisUlElement(c){rstText+="\n"rstText+=indentEachLine(ul2rst(c))}}return"- "+rstText+"\n"}funcul2rst(n*html.Node)string{rstText:=""forc:=n.FirstChild;c!=nil;c=c.NextSibling{ifisLiElement(c){rstText+=li2rst(c)}}returnrstText}functraverse(n*html.Node)string{ifisTextNode(n){returntextNode2rst(n)}ifisAnchorElement(n){returna2rst(n)}ifisUlElement(n){returnul2rst(n)}rstText:=""forc:=n.FirstChild;c!=nil;c=c.NextSibling{rstText+=traverse(c)}returnrstText}funcHtmlAUlLiToRst(rio.Reader)string{doc,err:=html.Parse(r)iferr!=nil{panic("Fail to parse html")}returntraverse(doc)}