Skip to content

Commit 7934b0c

Browse files
committed
#10: limit level of subdirctory probing
1 parent d7c575c commit 7934b0c

File tree

4 files changed

+72
-49
lines changed

4 files changed

+72
-49
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ OPTIONS:
3333

3434
```json
3535
{
36-
"main_css":"default.css",
37-
"code_css":"highlight.css",
38-
"port": 0,
39-
"quit": 9,
40-
"rev": 33
36+
"main_css": "default.css",
37+
"code_css": "highlight.css",
38+
"port": 0,
39+
"recurse": 2,
40+
"rev": 39
4141
}
4242
```
4343

@@ -46,7 +46,7 @@ Options are:
4646
* **main_css**: name of the primary stylesheet
4747
* **code_css**: stylesheet for [syntax highlighting](https://highlightjs.org/)
4848
* **port**: HTTP port, 0 means auto select
49-
* **quit**: delay before quit the local server
49+
* **recurse**: level of sub-directories to search for markdown files (1~9, default 2)
5050
* **rev**: revision of **MDV** that generated this config file
5151

5252
Usually, there is no need to edit this config file, except that you may want to use a new stylesheet. Alternatively, you can also edit the `default.css` file directly to tweak display effects. Also, there is a `highlight.css` which is the stylesheet for [syntax highlighting](https://highlightjs.org/).

collection.go

+63-42
Original file line numberDiff line numberDiff line change
@@ -23,57 +23,78 @@ func (c collection) CurrentPath() string {
2323
return filepath.Join(c.Path, c.CurrentFile())
2424
}
2525

26-
func collect(root string) (col *collection, err error) {
26+
func iter(path string, level int) []string {
27+
d, err := os.Open(path)
28+
assert(err)
29+
defer d.Close()
30+
fis, err := d.ReadDir(0)
31+
assert(err)
32+
sub := make(map[string][]string)
33+
var items []string
34+
for _, fi := range fis {
35+
fp := filepath.Join(path, fi.Name())
36+
if fi.IsDir() {
37+
if !strings.HasSuffix(fp, string(os.PathSeparator)) {
38+
fp += string(os.PathSeparator)
39+
}
40+
items = append(items, fp)
41+
if level > 0 {
42+
sub[fp] = iter(fp, level-1)
43+
}
44+
continue
45+
}
46+
ext := strings.ToLower(filepath.Ext(fi.Name()))
47+
if ext == ".md" {
48+
items = append(items, fp)
49+
}
50+
}
51+
sort.Strings(items)
52+
var files []string
53+
for _, fn := range items {
54+
if strings.HasSuffix(fn, string(os.PathSeparator)) {
55+
files = append(files, sub[fn]...)
56+
continue
57+
}
58+
files = append(files, fn)
59+
}
60+
return files
61+
}
62+
63+
func collect(entry string, depth int) (col *collection, err error) {
64+
if depth < 1 || depth > 9 {
65+
depth = 2
66+
}
2767
defer func() {
2868
if e := recover(); e != nil {
2969
err = e.(error)
3070
}
3171
}()
32-
root, err = filepath.Abs(root)
72+
entry, err = filepath.Abs(entry)
3373
assert(err)
34-
st, err := os.Stat(root)
74+
root := entry
75+
st, err := os.Stat(entry)
3576
assert(err)
36-
col = new(collection)
37-
col.Files = []string{}
38-
if st.IsDir() {
39-
col.Path = root
40-
col.Index = 0
41-
filepath.Walk(root, func(p string, fi os.FileInfo, e error) error {
42-
assert(err)
43-
if !fi.IsDir() && strings.ToLower(filepath.Ext(p)) == ".md" {
44-
col.Files = append(col.Files, p[len(col.Path)+1:])
45-
}
46-
return nil
47-
})
48-
} else {
49-
col.Path = filepath.Dir(root)
50-
col.Index = -1
51-
filepath.Walk(col.Path, func(p string, fi os.FileInfo, e error) error {
52-
assert(err)
53-
if !fi.IsDir() && strings.ToLower(filepath.Ext(p)) == ".md" {
54-
col.Files = append(col.Files, p[len(col.Path)+1:])
55-
}
56-
return nil
57-
})
77+
if !st.IsDir() {
78+
root = filepath.Dir(entry)
5879
}
59-
sort.Slice(col.Files, func(i, j int) bool {
60-
return strings.ToLower(col.Files[i]) < strings.ToLower(col.Files[j])
61-
})
62-
if col.Index < 0 {
63-
bn := filepath.Base(root)
64-
for i, f := range col.Files {
65-
if f == bn {
66-
col.Index = i
67-
break
80+
if !strings.HasSuffix(root, string(os.PathSeparator)) {
81+
root += string(os.PathSeparator)
82+
}
83+
files := iter(root, depth-1)
84+
var idx int
85+
if len(files) == 0 {
86+
idx = -1
87+
} else {
88+
for i := range files {
89+
if files[i] == entry {
90+
idx = i
6891
}
92+
files[i] = files[i][len(root):]
6993
}
70-
if col.Index < 0 {
71-
col.Files = append([]string{bn}, col.Files...)
72-
col.Index = 0
73-
}
74-
}
75-
if len(col.Files) == 0 {
76-
col.Index = -1
7794
}
78-
return col, nil
95+
return &collection{
96+
Path: root,
97+
Files: files,
98+
Index: idx,
99+
}, nil
79100
}

conf.go

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type (
1313
config struct {
1414
MainCSS string `json:"main_css"`
1515
CodeCSS string `json:"code_css"`
16+
Recurse int `json:"recurse"`
1617
Port int `json:"port"`
1718
Rev int `json:"rev"`
1819
dir string
@@ -58,6 +59,7 @@ var cf config
5859
func init() {
5960
cf.MainCSS = "default.css"
6061
cf.CodeCSS = "highlight.css"
62+
cf.Recurse = 2
6163
dir, err := os.UserConfigDir()
6264
if err != nil {
6365
fmt.Fprintln(os.Stderr, err)

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
root, _ := fs.Sub(res, "resources")
4747
extract(root, "default.css")
4848
extract(root, "highlight.css")
49-
col, err := collect(flag.Arg(0))
49+
col, err := collect(flag.Arg(0), cf.Recurse)
5050
if err != nil {
5151
fmt.Fprintln(os.Stderr, err)
5252
os.Exit(1)

0 commit comments

Comments
 (0)