blob: 53a6aeb24475457f3b20e38d41b89d841640a7cc (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
package main
/*
#include <stdio.h>
#include <stdlib.h>
#include "urlscanner.h"
*/
import "C"
import (
"html/template"
"log"
"strings"
)
type Link struct {
Url template.URL
Text template.HTML
IsDir bool
}
func getToken(input string) []Link {
var token C.int
var ls []Link
var l Link
C.scan_string(C.CString(input))
for token = C.yylex(); token != C.MYEOF; token = C.yylex() {
if token == C.TOKEN_URL {
// flex reads the href attr
l.Url = template.URL(C.GoString(C.yylval))
if strings.HasSuffix(C.GoString(C.yylval), "/") {
l.IsDir = true
}
} else if token == C.TOKEN_TEXT {
// flex reads the link description
l.Text = template.HTML(C.GoString(C.yylval))
ls = append(ls, l)
l = Link{}
} else {
// lexical error
l = Link{}
log.Printf("Lexical Error on line %d \n", C.yylineno)
continue
}
}
return ls
}
// sort
|