package main | |
import ( | |
"bufio" | |
"bytes" | |
"fmt" | |
) | |
const html = ` | |
<html> | |
<head> | |
<title>text</title> | |
</head> | |
<body> | |
<h1>more text</h1> | |
</body> | |
</html>` | |
func main() { | |
src := bytes.NewReader([]byte(html)) | |
scanner := bufio.NewScanner(src) | |
scanner.Split(splitHTML) | |
for scanner.Scan() { | |
fmt.Printf("token: %q\n", scanner.Text()) | |
} | |
fmt.Println(scanner.Err()) | |
} | |
func splitHTML(data []byte, atEOF bool) (advance int, token []byte, err error) { | |
tagStart := bytes.Index(data, []byte("<")) | |
if tagStart > 0 { | |
trimmed := bytes.TrimSpace(data[:tagStart]) | |
if len(trimmed) > 0 { | |
return tagStart, trimmed, nil | |
} | |
} | |
if tagStart == -1 { | |
if atEOF { | |
return 0, bytes.TrimSpace(data), bufio.ErrFinalToken | |
} else { | |
return 0, nil, nil | |
} | |
} | |
tagEnd := bytes.Index(data, []byte(">")) | |
if tagEnd == -1 { | |
if atEOF { | |
return 0, nil, fmt.Errorf("malformed tag: %q", string(data)) | |
} else { | |
return 0, nil, nil | |
} | |
} | |
return tagEnd + 1, data[tagStart : tagEnd+1], nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment