-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathmain.go
More file actions
161 lines (139 loc) · 3.99 KB
/
main.go
File metadata and controls
161 lines (139 loc) · 3.99 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright 2016 Google Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to writing, software distributed
// under the License is distributed on a "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
// embedmd
//
// embedmd embeds files or fractions of files into markdown files.
// It does so by searching embedmd commands, which are a subset of the
// markdown syntax for comments. This means they are invisible when
// markdown is rendered, so they can be kept in the file as pointers
// to the origin of the embedded text.
//
// The command receives a list of markdown files to process. At least one
// file must be provided; reading from standard input is not supported.
//
// embedmd supports two flags:
// -d: will print the difference of the input file with what the output
//
// would have been if executed.
//
// -w: rewrites the given files rather than writing the output to the standard
//
// output.
//
// For more information on the format of the commands, read the documentation
// of the github.com/campoy/embedmd/embedmd package.
package main
import (
"bytes"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"github.com/campoy/embedmd/embedmd"
"github.com/pmezard/go-difflib/difflib"
)
// modified while building by -ldflags.
var version = "unknown"
func usage() {
fmt.Fprintf(os.Stderr, "usage: embedmd [flags] [path ...]\n")
flag.PrintDefaults()
}
func main() {
rewrite := flag.Bool("w", false, "write result to (markdown) file instead of stdout")
doDiff := flag.Bool("d", false, "display diffs instead of rewriting files")
printVersion := flag.Bool("v", false, "display embedmd version")
flag.Usage = usage
flag.Parse()
if *printVersion {
fmt.Println("embedmd version: " + version)
return
}
diff, err := embed(flag.Args(), *rewrite, *doDiff)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
if diff && *doDiff {
os.Exit(2)
}
}
var stdout io.Writer = os.Stdout
func embed(paths []string, rewrite, doDiff bool) (foundDiff bool, err error) {
if rewrite && doDiff {
return false, fmt.Errorf("error: cannot use -w and -d simultaneously")
}
if len(paths) == 0 {
return false, fmt.Errorf("error: no markdown files provided")
}
for _, path := range paths {
d, err := processFile(path, rewrite, doDiff)
if err != nil {
return false, fmt.Errorf("%s:%v", path, err)
}
foundDiff = foundDiff || d
}
return foundDiff, nil
}
type file interface {
io.ReadCloser
io.WriterAt
Truncate(int64) error
}
// replaced by testing functions.
var openFile = func(name string) (file, error) {
return os.OpenFile(name, os.O_RDWR, 0666)
}
func processFile(path string, rewrite, doDiff bool) (foundDiff bool, err error) {
if filepath.Ext(path) != ".md" {
return false, fmt.Errorf("not a markdown file")
}
f, err := openFile(path)
if err != nil {
return false, err
}
defer f.Close()
var original bytes.Buffer
var r io.Reader = f
if doDiff {
r = io.TeeReader(f, &original)
}
buf := new(bytes.Buffer)
if err := embedmd.Process(buf, r, embedmd.WithBaseDir(filepath.Dir(path))); err != nil {
return false, err
}
if doDiff {
data, err := diff(original.String(), buf.String())
if err != nil || len(data) == 0 {
return false, err
}
fmt.Fprintf(stdout, "%s", data)
return true, nil
}
if rewrite {
n, err := f.WriteAt(buf.Bytes(), 0)
if err != nil {
return false, fmt.Errorf("could not write: %v", err)
}
return false, f.Truncate(int64(n))
}
_, err = io.Copy(stdout, buf)
return false, err
}
func diff(a, b string) (string, error) {
return difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
A: difflib.SplitLines(a),
B: difflib.SplitLines(b),
Context: 3,
})
}