Skip to content

Commit a97ce93

Browse files
committed
Allow opening local files by relative path
Local files can now be loaded by relative path, rather than the whole path preceded by the `file://` scheme indicator. It first checks whether or not the requested URL has a scheme (by checking for `://` in the URL). If not, it checks to see if the file exists before opening. It also allows making intention known by specifying `./` or `../` before the URL, closely matching the behavior of Firefox. This borrows code from @singalhimanshu's PR (makew0rld#232). Resolves makew0rld#231
1 parent e8122fc commit a97ce93

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

amfora.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"os"
7+
"path/filepath"
78
"strings"
89

910
"github.com/makeworld-the-better-one/amfora/bookmarks"
@@ -68,8 +69,25 @@ func main() {
6869
// Initialize Amfora's settings
6970
display.Init(version, commit, builtBy)
7071
display.NewTab()
72+
73+
// Load a URL, file, or render from stdin
7174
if len(os.Args[1:]) > 0 {
72-
display.URL(os.Args[1])
75+
url := os.Args[1]
76+
if !strings.Contains(url, "://") || strings.HasPrefix(url, "../") || strings.HasPrefix(url, "./") {
77+
fileName := url
78+
if _, err := os.Stat(fileName); err == nil {
79+
if !strings.HasPrefix(fileName, "/") {
80+
cwd, err := os.Getwd()
81+
if err != nil {
82+
fmt.Fprintf(os.Stderr, "error getting working directory path: %v\n", err)
83+
os.Exit(1)
84+
}
85+
fileName = filepath.Join(cwd, fileName)
86+
}
87+
url = "file://" + fileName
88+
}
89+
}
90+
display.URL(url)
7391
} else if !isStdinEmpty() {
7492
renderFromStdin()
7593
}

0 commit comments

Comments
 (0)