-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown.go
More file actions
57 lines (42 loc) · 1.26 KB
/
markdown.go
File metadata and controls
57 lines (42 loc) · 1.26 KB
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
import (
"net/url"
"os"
"regexp"
"strings"
"github.com/lunny/html2md"
)
// convert a web url to the local markdown url
func urlToMarkdownUri(base url.URL, uri url.URL) (name string, path string) {
relUrl := strings.TrimSuffix(strings.TrimPrefix(uri.Path, base.Path), "/")
orig_name := ""
if matches := regexp.MustCompile(`[^\/]([\w\d-_]+(\.[\w]+)?)?$`).FindAllString(relUrl, -1); len(matches) > 0 {
orig_name = matches[0]
if len(orig_name) > 0 && orig_name != "/" {
name = strings.Split(orig_name, ".")[0] + ".md"
}
}
if name == "" {
name = "index.md"
}
path = strings.TrimSuffix(relUrl, orig_name)
if len(path) == 0 {
path = "/"
}
return
}
// create a markdown file for the given url
// by convertign the body to markdown
func saveMarkdownFile(config _config, uri url.URL, body string) {
name, path := urlToMarkdownUri(config.rooturl, uri)
directory := config.storagedir + "/" + path
filename := directory + "/" + name
filename = strings.Replace(filename, "//", "/", -1)
config.fs.MkdirAll(directory, os.ModePerm)
file, err := config.fs.Create(filename)
defer func() { file.Close() }()
if err != nil {
panic(err)
}
file.WriteString(regexp.MustCompile(`[\n\s]*\n[\n\s]*`).ReplaceAllString(html2md.Convert(body), "\n"))
}