Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
2e8a74ec
Commit
2e8a74ec
authored
Aug 05, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
markdown: Prefix log messages, and slight refactor
Also change sha1 to md5 for the directory scans; slightly faster.
parent
e94e90b0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
59 deletions
+61
-59
config/setup/markdown.go
config/setup/markdown.go
+2
-2
middleware/markdown/generator.go
middleware/markdown/generator.go
+5
-3
middleware/markdown/markdown.go
middleware/markdown/markdown.go
+11
-13
middleware/markdown/page.go
middleware/markdown/page.go
+42
-40
middleware/markdown/process.go
middleware/markdown/process.go
+1
-1
No files found.
config/setup/markdown.go
View file @
2e8a74ec
...
...
@@ -25,12 +25,12 @@ func Markdown(c *Controller) (middleware.Middleware, error) {
IndexFiles
:
[]
string
{
"index.md"
},
}
//
For any configs that enabled static site gen, sweep the whole path at startup
//
Sweep the whole path at startup to at least generate link index, maybe generate static site
c
.
Startup
=
append
(
c
.
Startup
,
func
()
error
{
for
i
:=
range
mdconfigs
{
cfg
:=
&
mdconfigs
[
i
]
// Generate
static files.
// Generate
link index and static files (if enabled)
if
err
:=
markdown
.
GenerateStatic
(
md
,
cfg
);
err
!=
nil
{
return
err
}
...
...
middleware/markdown/generator.go
View file @
2e8a74ec
package
markdown
import
(
"crypto/
sha1
"
"crypto/
md5
"
"encoding/hex"
"fmt"
"io/ioutil"
...
...
@@ -13,7 +13,9 @@ import (
"github.com/mholt/caddy/middleware"
)
// GenerateStatic generate static files from markdowns.
// GenerateStatic generate static files and link index from markdowns.
// It only generates static files if it is enabled (cfg.StaticDir
// must be set).
func
GenerateStatic
(
md
Markdown
,
cfg
*
Config
)
error
{
generated
,
err
:=
generateLinks
(
md
,
cfg
)
if
err
!=
nil
{
...
...
@@ -130,6 +132,6 @@ func computeDirHash(md Markdown, c Config) (string, error) {
return
""
,
err
}
sum
:=
sha1
.
Sum
([]
byte
(
hashString
))
sum
:=
md5
.
Sum
([]
byte
(
hashString
))
return
hex
.
EncodeToString
(
sum
[
:
]),
nil
}
middleware/markdown/markdown.go
View file @
2e8a74ec
...
...
@@ -94,9 +94,8 @@ func (c Config) IsValidExt(ext string) bool {
// ServeHTTP implements the http.Handler interface.
func
(
md
Markdown
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
for
i
:=
range
md
.
Configs
{
m
:=
&
md
.
Configs
[
i
]
if
!
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
m
.
PathScope
)
{
for
_
,
cfg
:=
range
md
.
Configs
{
if
!
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
cfg
.
PathScope
)
{
continue
}
...
...
@@ -105,7 +104,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
fpath
=
idx
}
for
_
,
ext
:=
range
m
.
Extensions
{
for
_
,
ext
:=
range
cfg
.
Extensions
{
if
strings
.
HasSuffix
(
fpath
,
ext
)
{
f
,
err
:=
md
.
FileSys
.
Open
(
fpath
)
if
err
!=
nil
{
...
...
@@ -121,19 +120,18 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
}
// if development is set, scan directory for file changes for links.
if
m
.
Development
{
if
err
:=
GenerateStatic
(
md
,
m
);
err
!=
nil
{
log
.
Println
(
err
)
if
cfg
.
Development
{
if
err
:=
GenerateStatic
(
md
,
&
cfg
);
err
!=
nil
{
log
.
Println
(
"On-demand generation error (markdown):"
,
err
)
}
}
// if static site is generated, attempt to use it
if
filepath
,
ok
:=
m
.
StaticFiles
[
fpath
];
ok
{
if
filepath
,
ok
:=
cfg
.
StaticFiles
[
fpath
];
ok
{
if
fs1
,
err
:=
os
.
Stat
(
filepath
);
err
==
nil
{
// if markdown has not been modified
// since static page generation,
// serve the static page
if
fs
.
ModTime
()
.
UnixNano
()
<
fs1
.
ModTime
()
.
UnixNano
()
{
// if markdown has not been modified since static page
// generation, serve the static page
if
fs
.
ModTime
()
.
Before
(
fs1
.
ModTime
())
{
if
html
,
err
:=
ioutil
.
ReadFile
(
filepath
);
err
==
nil
{
w
.
Write
(
html
)
return
http
.
StatusOK
,
nil
...
...
@@ -156,7 +154,7 @@ func (md Markdown) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
Req
:
r
,
URL
:
r
.
URL
,
}
html
,
err
:=
md
.
Process
(
*
m
,
fpath
,
body
,
ctx
)
html
,
err
:=
md
.
Process
(
cfg
,
fpath
,
body
,
ctx
)
if
err
!=
nil
{
return
http
.
StatusInternalServerError
,
err
}
...
...
middleware/markdown/page.go
View file @
2e8a74ec
...
...
@@ -92,7 +92,7 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool {
l
.
Unlock
()
return
false
}
else
if
err
!=
nil
{
log
.
Println
(
"
Error
:"
,
err
)
log
.
Println
(
"
Hash error (markdown)
:"
,
err
)
}
cfg
.
Links
=
[]
PageLink
{}
...
...
@@ -100,7 +100,10 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool {
cfg
.
Lock
()
l
.
lastErr
=
filepath
.
Walk
(
fp
,
func
(
path
string
,
info
os
.
FileInfo
,
err
error
)
error
{
for
_
,
ext
:=
range
cfg
.
Extensions
{
if
!
info
.
IsDir
()
&&
strings
.
HasSuffix
(
info
.
Name
(),
ext
)
{
if
info
.
IsDir
()
||
!
strings
.
HasSuffix
(
info
.
Name
(),
ext
)
{
continue
}
// Load the file
body
,
err
:=
ioutil
.
ReadFile
(
path
)
if
err
!=
nil
{
...
...
@@ -147,7 +150,6 @@ func (l *linkGen) generateLinks(md Markdown, cfg *Config) bool {
break
// don't try other file extensions
}
}
return
nil
})
...
...
middleware/markdown/process.go
View file @
2e8a74ec
...
...
@@ -114,7 +114,7 @@ func (md Markdown) processTemplate(c Config, requestPath string, tmpl []byte, me
// if static page generation fails,
// nothing fatal, only log the error.
// TODO: Report this non-fatal error, but don't log it here
log
.
Println
(
err
)
log
.
Println
(
"Rendering error (markdown):"
,
err
)
}
return
b
.
Bytes
(),
nil
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment