-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.go
127 lines (97 loc) · 2.76 KB
/
app.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/dunglas/calavera/extractor"
"github.com/dunglas/calavera/schema"
)
const filePerms = 0644
const dirPerms = 0755
func main() {
inputPath, outputPath, prettify := parseFlags()
var files []string
var extractors = []extractor.Extractor{extractor.NewMarkdown(inputPath)}
if ge, err := extractor.NewGit(inputPath); nil == err {
extractors = append(extractors, ge)
} else {
log.Println(`"` + inputPath + `" is not a Git repository. Authors and date metadata will NOT be extracted.`)
}
walkFunc := func(path string, _ os.FileInfo, err error) error {
if nil != err || !strings.HasSuffix(path, ".md") {
return nil
}
abs, err := filepath.Abs(path)
check(err)
rel, err := filepath.Rel(inputPath, abs)
check(err)
files = append(files, rel)
return nil
}
if err := filepath.Walk(inputPath, walkFunc); nil != err {
check(err)
}
entrypoint := schema.NewItemList()
for _, file := range files {
// Cannot use a go routine because src-d/go-git isn't thread safe
convert(file, outputPath, extractors, prettify)
entrypoint.Element = append(entrypoint.Element, getOutputPath(file))
}
check(ioutil.WriteFile(outputPath+"/_index.jsonld", marshal(entrypoint, prettify), filePerms))
}
func marshal(v interface{}, prettify bool) []byte {
var jsonContent []byte
var err error
if prettify {
jsonContent, err = json.MarshalIndent(v, "", "\t")
} else {
jsonContent, err = json.Marshal(v)
}
check(err)
return jsonContent
}
func check(err error) {
if nil == err {
return
}
log.Fatalln(err)
panic(err)
}
func parseFlags() (string, string, bool) {
flag.Usage = func() {
fmt.Println("calavera input_directory output_directory")
}
prettify := flag.Bool("prettify", false, "Prettify json output")
flag.Parse()
if len(flag.Args()) != 2 {
log.Fatalln("Input and output directories are mandatory arguments.")
}
inputPath, err := filepath.Abs(flag.Arg(0))
check(err)
outputPath, err := filepath.Abs(flag.Arg(1))
check(err)
return inputPath, outputPath, *prettify
}
func convert(path string, outputDirectory string, extractors []extractor.Extractor, prettify bool) {
creativeWork := schema.NewCreativeWork()
creativeWork.Id = getOutputPath(path)
for _, extractor := range extractors {
err := extractor.Extract(creativeWork, path)
check(err)
}
jsonContent := marshal(creativeWork, prettify)
outputPath := outputDirectory + "/" + creativeWork.Id
outputSubdirectory := filepath.Dir(outputPath)
err := os.MkdirAll(outputSubdirectory, dirPerms)
check(err)
err = ioutil.WriteFile(outputPath, jsonContent, filePerms)
check(err)
}
func getOutputPath(originalPath string) string {
return originalPath[:len(originalPath)-3] + ".jsonld"
}