Exemplo n.º 1
0
func Annotate(src []byte, a syntaxhighlight.Annotator) (annotate.Annotations, error) {
	var s scanner.Scanner
	fset := token.NewFileSet()
	file := fset.AddFile("", fset.Base(), len(src))
	s.Init(file, src, nil, scanner.ScanComments)

	var anns annotate.Annotations

	for {
		pos, tok, lit := s.Scan()
		if tok == token.EOF {
			break
		}

		offset := fset.Position(pos).Offset

		var tokString string
		if lit != "" {
			tokString = lit
		} else {
			tokString = tok.String()
		}

		// TODO: Clean this up.
		//if tok == token.SEMICOLON {
		if tok == token.SEMICOLON && lit == "\n" {
			continue
		}

		ann, err := a.Annotate(offset, TokenKind(tok, lit), tokString)
		if err != nil {
			return nil, err
		}
		if ann != nil {
			anns = append(anns, ann)
		}
	}

	return anns, nil
}
Exemplo n.º 2
0
func Annotate(src []byte) (annotate.Annotations, error) {
	var a syntaxhighlight.Annotator = HTMLAnnotator(gfmDiff)
	s := NewScanner(src)

	var anns annotate.Annotations
	read := 0
	for s.Scan() {
		tok, kind := s.Token()
		ann, err := a.Annotate(read, kind, string(tok))
		if err != nil {
			return nil, err
		}
		read += len(tok)
		if ann != nil {
			anns = append(anns, ann)
		}
	}

	if err := s.Err(); err != nil {
		return nil, err
	}

	return anns, nil
}