Skip to content

Commit 68775d0

Browse files
authored
Move internals into internal directory (#49)
1 parent 7291b4a commit 68775d0

File tree

5 files changed

+57
-35
lines changed

5 files changed

+57
-35
lines changed

cmd/gh-md-toc/main.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package main
22

33
import (
4-
"fmt"
54
"io"
65
"os"
76

87
"gopkg.in/alecthomas/kingpin.v2"
98

109
ghtoc "github.com/ekalinin/github-markdown-toc.go"
10+
"github.com/ekalinin/github-markdown-toc.go/internal"
1111
)
1212

1313
var (
@@ -51,10 +51,7 @@ func processPaths() {
5151
}
5252

5353
if !*hideHeader && pathsCount == 1 {
54-
fmt.Println()
55-
fmt.Println("Table of Contents")
56-
fmt.Println("=================")
57-
fmt.Println()
54+
internal.ShowHeader(os.Stdout)
5855
}
5956

6057
for i := 1; i <= pathsCount; i++ {
@@ -83,7 +80,7 @@ func processSTDIN() {
8380

8481
// Entry point
8582
func main() {
86-
kingpin.Version(ghtoc.Version)
83+
kingpin.Version(internal.Version)
8784
kingpin.Parse()
8885

8986
if *token == "" {
@@ -101,6 +98,6 @@ func main() {
10198
}
10299

103100
if !*hideFooter {
104-
fmt.Println("Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc.go)")
101+
internal.ShowFooter(os.Stdout)
105102
}
106103
}

ghdoc.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"regexp"
1010
"strconv"
1111
"strings"
12+
13+
"github.com/ekalinin/github-markdown-toc.go/internal"
1214
)
1315

1416
// Print TOC to the console
@@ -59,8 +61,8 @@ func NewGHDoc(Path string, AbsPaths bool, StartDepth int, Depth int, Escape bool
5961
Debug: Debug,
6062
html: "",
6163
logger: log.New(os.Stderr, "", log.LstdFlags),
62-
httpGetter: httpGet,
63-
httpPoster: httpPost,
64+
httpGetter: internal.HttpGet,
65+
httpPoster: internal.HttpPost,
6466
ghURL: "https://api.github.com",
6567
}
6668
}
@@ -114,7 +116,7 @@ func (doc *GHDoc) Convert2HTML() error {
114116
// if not a plain text - return the result (should be html)
115117
if strings.Split(ContentType, ";")[0] != "text/plain" {
116118
doc.html = string(htmlBody)
117-
doc.d("Convert2HTML: not a plain text, body" + string(htmlBody)[:200])
119+
doc.d("Convert2HTML: not a plain text, body")
118120
return nil
119121
}
120122

@@ -163,7 +165,7 @@ func (doc *GHDoc) GrabToc() *GHToc {
163165
`href="(?P<href>[^"]+)">\s*` +
164166
`(?P<name>.*?)<span`
165167
r := regexp.MustCompile(re)
166-
listIndentation := generateListIndentation(doc.Indent)
168+
listIndentation := internal.GenerateListIndentation(doc.Indent)
167169

168170
toc := GHToc{}
169171
minHeaderNum := 6
@@ -178,7 +180,7 @@ func (doc *GHDoc) GrabToc() *GHToc {
178180
continue
179181
}
180182
doc.d("GrabToc: process group: " + name + ": " + match[i] + " ...")
181-
group[name] = removeStuff(match[i])
183+
group[name] = internal.RemoveStuff(match[i])
182184
}
183185
// update minimum header number
184186
n, _ := strconv.Atoi(group["num"])
@@ -206,9 +208,9 @@ func (doc *GHDoc) GrabToc() *GHToc {
206208
link = doc.Path + link
207209
}
208210

209-
tmpSection = removeStuff(group["name"])
211+
tmpSection = internal.RemoveStuff(group["name"])
210212
if doc.Escape {
211-
tmpSection = EscapeSpecChars(tmpSection)
213+
tmpSection = internal.EscapeSpecChars(tmpSection)
212214
}
213215
tocItem := strings.Repeat(listIndentation(), n-minHeaderNum-doc.StartDepth) + "* " +
214216
"[" + tmpSection + "]" +

internals.go renamed to internal/utils.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
package ghtoc
1+
package internal
22

33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"io"
78
"net/http"
89
"os"
910
"strings"
1011
)
1112

12-
const (
13-
// Version is a current app version
14-
Version = "1.3.0"
15-
userAgent = "github-markdown-toc.go v" + Version
16-
)
17-
1813
// doHTTPReq executes a particular http request
1914
func doHTTPReq(req *http.Request) ([]byte, string, error) {
2015
req.Header.Set("User-Agent", userAgent)
@@ -37,17 +32,27 @@ func doHTTPReq(req *http.Request) ([]byte, string, error) {
3732
return body, resp.Header.Get("Content-type"), nil
3833
}
3934

40-
// Executes HTTP GET request
41-
func httpGet(urlPath string) ([]byte, string, error) {
35+
// HttpGet executes HTTP GET request.
36+
func HttpGet(urlPath string) ([]byte, string, error) {
37+
req, err := http.NewRequest("GET", urlPath, nil)
38+
if err != nil {
39+
return []byte{}, "", err
40+
}
41+
return doHTTPReq(req)
42+
}
43+
44+
func HttpGetJson(urlPath string) ([]byte, string, error) {
4245
req, err := http.NewRequest("GET", urlPath, nil)
46+
req.Header.Set("Content-type", "application/json")
47+
req.Header.Set("Accept", "application/json")
4348
if err != nil {
4449
return []byte{}, "", err
4550
}
4651
return doHTTPReq(req)
4752
}
4853

49-
// httpPost executes HTTP POST with file content
50-
func httpPost(urlPath, filePath, token string) (string, error) {
54+
// HttpPost executes HTTP POST with file content.
55+
func HttpPost(urlPath, filePath, token string) (string, error) {
5156
file, err := os.Open(filePath)
5257
if err != nil {
5358
return "", err
@@ -74,8 +79,8 @@ func httpPost(urlPath, filePath, token string) (string, error) {
7479
return string(resp), err
7580
}
7681

77-
// removeStuff trims spaces, removes new lines and code tag from a string
78-
func removeStuff(s string) string {
82+
// RemoveStuff trims spaces, removes new lines and code tag from a string.
83+
func RemoveStuff(s string) string {
7984
res := strings.Replace(s, "\n", "", -1)
8085
res = strings.Replace(res, "<code>", "", -1)
8186
res = strings.Replace(res, "</code>", "", -1)
@@ -84,15 +89,13 @@ func removeStuff(s string) string {
8489
return res
8590
}
8691

87-
// generate func of custom spaces indentation
88-
func generateListIndentation(spaces int) func() string {
92+
// Generate func of custom spaces indentation.
93+
func GenerateListIndentation(spaces int) func() string {
8994
return func() string {
9095
return strings.Repeat(" ", spaces)
9196
}
9297
}
9398

94-
// Public
95-
9699
// EscapeSpecChars Escapes special characters
97100
func EscapeSpecChars(s string) string {
98101
specChar := []string{"\\", "`", "*", "_", "{", "}", "#", "+", "-", ".", "!"}
@@ -103,3 +106,16 @@ func EscapeSpecChars(s string) string {
103106
}
104107
return res
105108
}
109+
110+
// ShowHeader shows header befor TOC.
111+
func ShowHeader(w io.Writer) {
112+
fmt.Fprintln(w)
113+
fmt.Fprintln(w, "Table of Contents")
114+
fmt.Fprintln(w, "=================")
115+
fmt.Fprintln(w)
116+
}
117+
118+
// ShowFooter shows footer after TOC.
119+
func ShowFooter(w io.Writer) {
120+
fmt.Fprintln(w, "Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc.go)")
121+
}

internal_test.go renamed to internal/utils_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ghtoc
1+
package internal
22

33
import (
44
"fmt"
@@ -19,7 +19,7 @@ func TestHttpGet(t *testing.T) {
1919
}))
2020
defer srv.Close()
2121

22-
body, _, err := httpGet(srv.URL)
22+
body, _, err := HttpGet(srv.URL)
2323
got := string(body)
2424

2525
if err != nil {
@@ -41,7 +41,7 @@ func TestHttpGetForbidden(t *testing.T) {
4141
}))
4242
defer srv.Close()
4343

44-
_, _, err := httpGet(srv.URL)
44+
_, _, err := HttpGet(srv.URL)
4545
if err == nil {
4646
t.Error("Should not not be nil")
4747
}
@@ -86,7 +86,7 @@ func TestHttpPost(t *testing.T) {
8686
}
8787
defer os.Remove(fileName)
8888

89-
_, err = httpPost(srv.URL, fileName, token)
89+
_, err = HttpPost(srv.URL, fileName, token)
9090
if err != nil {
9191
t.Error("Should not be err", err)
9292
}

internal/version.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package internal
2+
3+
const (
4+
// Version is a current app version
5+
Version = "1.3.0"
6+
userAgent = "github-markdown-toc.go v" + Version
7+
)

0 commit comments

Comments
 (0)