-
Notifications
You must be signed in to change notification settings - Fork 2
/
file_markup.go
71 lines (57 loc) · 1.41 KB
/
file_markup.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
// SPDX-FileCopyrightText: 2019 Shulhan <ms@kilabit.info>
// SPDX-License-Identifier: GPL-3.0-or-later
package ciigo
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// List of markup kind.
const (
markupKindAdoc = 1
markupKindMarkdown = 2
)
// FileMarkup contains the markup path and its kind.
type FileMarkup struct {
info os.FileInfo // info contains FileInfo of markup file.
basePath string // Full path to file without markup extension.
path string // Full path to markup file.
pathHTML string // path to HTML file.
kind int
}
// NewFileMarkup create new FileMarkup instance form file in "filePath".
// The "fi" option is optional, if its nil it will Stat-ed manually.
func NewFileMarkup(filePath string, fi os.FileInfo) (fmarkup *FileMarkup, err error) {
var (
logp = `NewFileMarkup`
ext string
)
if len(filePath) == 0 {
return nil, fmt.Errorf(`%s: empty path`, logp)
}
if fi == nil {
fi, err = os.Stat(filePath)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}
}
ext = strings.ToLower(filepath.Ext(filePath))
fmarkup = &FileMarkup{
path: filePath,
info: fi,
basePath: strings.TrimSuffix(filePath, ext),
kind: markupKind(ext),
}
fmarkup.pathHTML = fmarkup.basePath + `.html`
return fmarkup, nil
}
func markupKind(ext string) int {
switch ext {
case extAsciidoc:
return markupKindAdoc
case extMarkdown:
return markupKindMarkdown
}
return 0
}