mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-05 14:03:45 +00:00
37 lines
651 B
Go
37 lines
651 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 123.
|
|
|
|
// Outline prints the outline of an HTML document tree.
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"golang.org/x/net/html"
|
|
)
|
|
|
|
//!+
|
|
func main() {
|
|
doc, err := html.Parse(os.Stdin)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "outline: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
outline(nil, doc)
|
|
}
|
|
|
|
func outline(stack []string, n *html.Node) {
|
|
if n.Type == html.ElementNode {
|
|
stack = append(stack, n.Data) // push tag
|
|
fmt.Println(stack)
|
|
}
|
|
for c := n.FirstChild; c != nil; c = c.NextSibling {
|
|
outline(stack, c)
|
|
}
|
|
}
|
|
|
|
//!-
|