diff --git a/examples/html.feature b/examples/html.feature
index 83d8be1..4e29502 100644
--- a/examples/html.feature
+++ b/examples/html.feature
@@ -76,3 +76,22 @@ Feature: HTML
"""
When I run `liche foo.html`
Then the exit status should be 1
+
+ Scenario: Set document root to a sub directory
+ Given a directory named "sub"
+ And a file named "sub/foo.html" with:
+ """
+
+
+
+ My title
+
+
+
+
+
+ """
+ When I successfully run `liche --document-root sub sub/foo.html`
+ Then the stderr should contain exactly ""
diff --git a/url_checker.go b/url_checker.go
index 7fab12d..47934d9 100644
--- a/url_checker.go
+++ b/url_checker.go
@@ -5,7 +5,6 @@ import (
"net/url"
"os"
"path"
- "strings"
"sync"
"time"
@@ -23,7 +22,7 @@ func newURLChecker(t time.Duration, d string, s semaphore) urlChecker {
}
func (c urlChecker) Check(u string, f string) error {
- u, err := c.resolveURL(u)
+ u, err := c.resolveURL(u, f)
if err != nil {
return err
@@ -36,7 +35,8 @@ func (c urlChecker) Check(u string, f string) error {
}
if uu.Scheme == "" {
- return checkRelativePath(u, f)
+ _, err := os.Stat(uu.Path)
+ return err
}
c.semaphore.Request()
@@ -67,19 +67,24 @@ func (c urlChecker) CheckMany(us []string, f string, rc chan<- urlResult) {
close(rc)
}
-func (c urlChecker) resolveURL(u string) (string, error) {
- abs := strings.HasPrefix(u, "/")
+func (c urlChecker) resolveURL(u string, f string) (string, error) {
+ uu, err := url.Parse(u)
- if abs && c.documentRoot != "" {
- return path.Join(c.documentRoot, u), nil
- } else if abs {
- return "", errors.New("document root directory is not specified")
+ if err != nil {
+ return "", err
}
- return u, nil
-}
+ if uu.Scheme != "" {
+ return u, nil
+ }
-func checkRelativePath(p string, f string) error {
- _, err := os.Stat(path.Join(path.Dir(f), p))
- return err
+ if !path.IsAbs(uu.Path) {
+ return path.Join(path.Dir(f), uu.Path), nil
+ }
+
+ if c.documentRoot == "" {
+ return "", errors.New("document root directory is not specified")
+ }
+
+ return path.Join(c.documentRoot, uu.Path), nil
}
diff --git a/url_checker_test.go b/url_checker_test.go
index 8afd0c2..e0df8b0 100644
--- a/url_checker_test.go
+++ b/url_checker_test.go
@@ -51,7 +51,7 @@ func TestURLCheckerResolveURL(t *testing.T) {
{"foo", "foo"},
{"https://google.com", "https://google.com"},
} {
- u, err := f.resolveURL(c.source)
+ u, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)
@@ -61,7 +61,7 @@ func TestURLCheckerResolveURL(t *testing.T) {
func TestURLCheckerResolveURLWithAbsolutePath(t *testing.T) {
f := newURLChecker(0, "", newSemaphore(1024))
- u, err := f.resolveURL("/foo")
+ u, err := f.resolveURL("/foo", "foo.md")
assert.NotEqual(t, nil, err)
assert.Equal(t, "", u)
@@ -75,7 +75,7 @@ func TestURLCheckerResolveURLWithDocumentRoot(t *testing.T) {
{"https://google.com", "https://google.com"},
{"/foo", "foo/foo"},
} {
- u, err := f.resolveURL(c.source)
+ u, err := f.resolveURL(c.source, "foo.md")
assert.Equal(t, nil, err)
assert.Equal(t, c.target, u)