-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static Local Website with Hugo (file:///) #622
Comments
This is an interesting question, but there is an alternative solution. If it were possible to generate all the content using only relative URLs, then it would not matter whether you had a I tried setting baseurl to "", but this doesn't help you (not at the moment, at least). It sets all the intra-content URLs to start with I would wholeheartedly support a change to Hugo that generates content with nothing but relative URLs. It should be quite easy to achieve in principle - Hugo knows where everything is. This would be a more general solution to your use-case than merely making Rick |
I've discovered that setting baseurl to "" creates a website with some broken menu links :-( Hugo cannot deal with this use-case at present, as far as I can tell. |
Yeah I had noticed that as well. It would be great if we could get the relative references working with setting the basurl to "". |
I created a VBS script that will automatically make all links created by hugo relative. This works for anchor tags to other posts as well as links to static files (css, js, img). Place the file in the parent directory of the 'public' directory.
'relative-reference.vbs
'specifiy the website in config.toml as a command parameter when calling this vbs script. Make sure there is ending / on website
Const ForReading = 1, ForWriting = 2, ForAppending = 8
pub_dir = "public"
levelPaths = Array("","../","../../","../../../") '4 level for relative reference'
'Command line argument for websitre url'
if WScript.Arguments.Count = 0 then
badPath = "http://localhost:1318/"
else
badPath = WScript.Arguments(0)
end if
'File system object for file manipulation'
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = objFSO.GetAbsolutePathName(".") & "/" & pub_dir
ShowSubfolders objFSO.GetFolder(objStartFolder), 0 'Start off in the public directory, level 0'
Sub ShowSubFolders(Folder, level)
Set colFiles = Folder.Files
For Each objFile in colFiles
if isHTML(objFile.Path) then 'check if file is HTML'
localize objFile,level 'make local references in file'
end if
Next
For Each Subfolder in Folder.SubFolders
'Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
ShowSubFolders Subfolder, level +1 'Recursive call, increment the level'
Next
End Sub
Sub localize(file, level)
'Wscript.echo "test"
'create object file for reading, this is the HTML file hugo created'
set myFile = objFSO.OpenTextFile(file.Path,ForReading,True)
newFile = myFile.ReadAll 'read the html file and store as stirng'
myFile.Close 'close the file'
'THe following uses regular expressions to find a match for links to other posts (<a href)'
Dim regEx 'Regular Expression object'
Dim colMatches 'will contain matches for links'
' Create regular expression.
Set regEx = New RegExp
regEx.Pattern = badPath & "(.*?"")" 'regular expression that captures website url and internal link inside
regEx.IgnoreCase = True
regEx.global = true
Set colMatches = regEx.Execute(newFile) ' Execute search.
For Each objMatch In colMatches ' Iterate Matches collection.
fullMatch = objMatch.Value 'This is the full match, includes the full tag'
subMatch = objMatch.SubMatches(0) 'This is the link inside of the tag'
replaceMatch = replace(fullMatch,badPath,levelPaths(level)) 'puts relative reference inside the tag'
if(isFile(subMatch)) then 'Checks if this rerference is to a css/js/html file'
newFile = replace(newFile,fullMatch,replaceMatch)
elseif right(subMatch,2) = "/""" then 'check if there is an ending /, if not then add it with index.html'
newFile = Replace(newFile,fullMatch, left(replaceMatch,len(replaceMatch)-1) & "index.html""")
else 'There is no ending / so you must add it here'
newFile = Replace(newFile,fullMatch, left(replaceMatch,len(replaceMatch)-1) & "/index.html""")
end if
Next
'Take care of home page references'
bad_index = "href=""" & badPath
good_index = "href=""" & levelPaths(level) & "index.html"
newFile = replace(newFile,left(bad_index,len(bad_index)-1),good_index)
set myFile = objFSO.OpenTextFile(file.Path,ForWriting,True)
myFile.Write newFile 'write the html file
myFile.Close
end Sub
Function isHTML (file)
if right(file,4) = "html" then
isHTML = True
else
isHTML = false
end if
End Function
Function isFile(myString)
Dim regEx, retVal
' Create regular expression.
Set regEx = New RegExp
regEx.Pattern = ".*\.[a-zA-Z]{2,4}"
regEx.IgnoreCase = False
isFile = regEx.Test(myString)
end Function |
This is a noble effort, but... It would be good to fix the Hugo source code and submit a patch, instead of trying to fix links after they've been created. |
Hi @JeremyBYU and @rickb777, It turns out this issue is very similar to #347 where the user wanted to use So, @JeremyBYU, with Hugo v0.13-DEV, you may now use a Please let us know how it works out for you! |
Temporary workaround for the bug fix and resulting behavioral change in purell.NormalizeURLString(): a leading '/' was inadvertently to relative links, but no longer, see gohugoio#878. I think the real solution is to allow Hugo to make relative URL with relative path, e.g. "../../post/hello-again/", as wished by users in issues #157, #622, etc., without forcing relative URLs to begin with '/'. Once the fixes are in, let's remove this kludge and restore SanitizeUrl() to the way it was. Fixes gohugoio#878
Temporary workaround for the bug fix and resulting behavioral change in purell.NormalizeURLString(): a leading '/' was inadvertently to relative links, but no longer, see #878. I think the real solution is to allow Hugo to make relative URL with relative path, e.g. "../../post/hello-again/", as wished by users in issues #157, #622, etc., without forcing relative URLs to begin with '/'. Once the fixes are in, let's remove this kludge and restore SanitizeUrl() to the way it was. Fixes #878
This safeUrl solution seems like an ugly hack. Why is it not possible to construct path-relative URLs so that safeUrl is not necessary? |
Hi @rickb777, I think having a way to construct path-relative URLs is a worthy goal for v0.14 or v0.15. I agree that using But yes, not only do we welcome comments and suggestions, we welcome Pull Requests even more! So, if you have time, we would love to receive a PR from you! 😉 |
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in /index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). THIS WORKS, BUT NEEDS SOME POLISH. WORK IN PROGRESS. Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). THIS WORKS, BUT NEEDS SOME POLISH. WORK IN PROGRESS. Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). THIS WORKS, BUT NEEDS SOME POLISH. WORK IN PROGRESS. The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 24135 24777 +2.66% BenchmarkAbsURLSrcset 26345 26803 +1.74% BenchmarkXMLAbsURLSrcset 25956 27344 +5.35% BenchmarkXMLAbsURL 13089 13231 +1.08% benchmark old allocs new allocs delta BenchmarkAbsURL 24 24 +0.00% BenchmarkAbsURLSrcset 29 29 +0.00% BenchmarkXMLAbsURLSrcset 27 27 +0.00% BenchmarkXMLAbsURL 12 12 +0.00% benchmark old bytes new bytes delta BenchmarkAbsURL 3183 3218 +1.10% BenchmarkAbsURLSrcset 2373 2428 +2.32% BenchmarkXMLAbsURLSrcset 2559 2611 +2.03% BenchmarkXMLAbsURL 1878 1916 +2.02% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 24135 24777 +2.66% BenchmarkAbsURLSrcset 26345 26803 +1.74% BenchmarkXMLAbsURLSrcset 25956 27344 +5.35% BenchmarkXMLAbsURL 13089 13231 +1.08% benchmark old allocs new allocs delta BenchmarkAbsURL 24 24 +0.00% BenchmarkAbsURLSrcset 29 29 +0.00% BenchmarkXMLAbsURLSrcset 27 27 +0.00% BenchmarkXMLAbsURL 12 12 +0.00% benchmark old bytes new bytes delta BenchmarkAbsURL 3183 3218 +1.10% BenchmarkAbsURLSrcset 2373 2428 +2.32% BenchmarkXMLAbsURLSrcset 2559 2611 +2.03% BenchmarkXMLAbsURL 1878 1916 +2.02% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 24135 24777 +2.66% BenchmarkAbsURLSrcset 26345 26803 +1.74% BenchmarkXMLAbsURLSrcset 25956 27344 +5.35% BenchmarkXMLAbsURL 13089 13231 +1.08% benchmark old allocs new allocs delta BenchmarkAbsURL 24 24 +0.00% BenchmarkAbsURLSrcset 29 29 +0.00% BenchmarkXMLAbsURLSrcset 27 27 +0.00% BenchmarkXMLAbsURL 12 12 +0.00% benchmark old bytes new bytes delta BenchmarkAbsURL 3183 3218 +1.10% BenchmarkAbsURLSrcset 2373 2428 +2.32% BenchmarkXMLAbsURLSrcset 2559 2611 +2.03% BenchmarkXMLAbsURL 1878 1916 +2.02% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 24135 24777 +2.66% BenchmarkAbsURLSrcset 26345 26803 +1.74% BenchmarkXMLAbsURLSrcset 25956 27344 +5.35% BenchmarkXMLAbsURL 13089 13231 +1.08% benchmark old allocs new allocs delta BenchmarkAbsURL 24 24 +0.00% BenchmarkAbsURLSrcset 29 29 +0.00% BenchmarkXMLAbsURLSrcset 27 27 +0.00% BenchmarkXMLAbsURL 12 12 +0.00% benchmark old bytes new bytes delta BenchmarkAbsURL 3183 3218 +1.10% BenchmarkAbsURLSrcset 2373 2428 +2.32% BenchmarkXMLAbsURLSrcset 2559 2611 +2.03% BenchmarkXMLAbsURL 1878 1916 +2.02% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 24135 24777 +2.66% BenchmarkAbsURLSrcset 26345 26803 +1.74% BenchmarkXMLAbsURLSrcset 25956 27344 +5.35% BenchmarkXMLAbsURL 13089 13231 +1.08% benchmark old allocs new allocs delta BenchmarkAbsURL 24 24 +0.00% BenchmarkAbsURLSrcset 29 29 +0.00% BenchmarkXMLAbsURLSrcset 27 27 +0.00% BenchmarkXMLAbsURL 12 12 +0.00% benchmark old bytes new bytes delta BenchmarkAbsURL 3183 3218 +1.10% BenchmarkAbsURLSrcset 2373 2428 +2.32% BenchmarkXMLAbsURLSrcset 2559 2611 +2.03% BenchmarkXMLAbsURL 1878 1916 +2.02% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622 Fixes gohugoio#937 Fixes #157
Temporary workaround for the bug fix and resulting behavioral change in purell.NormalizeURLString(): a leading '/' was inadvertently to relative links, but no longer, see gohugoio#878. I think the real solution is to allow Hugo to make relative URL with relative path, e.g. "../../post/hello-again/", as wished by users in issues gohugoio#157, gohugoio#622, etc., without forcing relative URLs to begin with '/'. Once the fixes are in, let's remove this kludge and restore SanitizeUrl() to the way it was. Fixes gohugoio#878
Setting `RelativeURLs` to `true` will make all relative URLs in the site *really* relative. And will do so with speed. So: In `/post/myblogpost.html`: `/mycss.css` becomes `../mycss.css` The same in `/index.html` will become: `./mycss.css` etc. Note that absolute URLs will not be touched (either external resources, or URLs constructed with `BaseURL`). The speediness is about the same as before: ``` benchmark old ns/op new ns/op delta BenchmarkAbsURL 17462 18164 +4.02% BenchmarkAbsURLSrcset 18842 19632 +4.19% BenchmarkXMLAbsURLSrcset 18643 19313 +3.59% BenchmarkXMLAbsURL 9283 9656 +4.02% benchmark old allocs new allocs delta BenchmarkAbsURL 24 28 +16.67% BenchmarkAbsURLSrcset 29 32 +10.34% BenchmarkXMLAbsURLSrcset 27 30 +11.11% BenchmarkXMLAbsURL 12 14 +16.67% benchmark old bytes new bytes delta BenchmarkAbsURL 3154 3404 +7.93% BenchmarkAbsURLSrcset 2376 2573 +8.29% BenchmarkXMLAbsURLSrcset 2569 2763 +7.55% BenchmarkXMLAbsURL 1888 1998 +5.83% ``` Fixes gohugoio#1104 Fixes gohugoio#622 Fixes gohugoio#937 Fixes gohugoio#157
This is not that uncommon actually. Another use case is letting users download a html site (e.g. docs for a software project) as a bundle to use offline. This issue was closed, but the behavior is still not supported (nor documented as far as I can tell). Using something like:
will not result in a working site whether Can this issue be reopened, or closed as wontfix? |
Update: after a couple of hours of digging I found the answer: So no need to reopen this. I will see if I can find time for a PR to improve the Hugo docs - searching for terms like "portable", "offline", "deploying to a shared drive", etc. should lead users to a description of this behavior. |
I tried:
But it doesn't work for me. Couldn't we simply create a |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hey everyone,
This question will probably seem a little strange to most people because most people dont deploy websites by distributing a folder of html files. However I sometimes do this at work (with a network file share) instead of having to deal with a server.
What I'm trying to do is create a website with hugo that will build all my html files for a simple website and be able to open the html files with a browser. To accomplish this I set
baseurl = path_to_file_share
. It correctly links all resources in the html files (css, images, javascripts). I open the index.html homepage file in the file share and it loads correctly. However all the link to other posts are broken and are replaced with<a href="#ZgotmplZ">
A little googling brought me to this page which basically says that Go is considering the html to be unsafe and is escaping it. I'm guessing it is the file protocol in the link that it is considering to be unsafe (e.g. file:///).
I know that there are some ways in the Go templating language to disable this functionality (see link). Is there any way we can put a flag in hugo (maybe the config.toml) that will inactivate this html escaping feature.
Thanks for your help
Jeremy
The text was updated successfully, but these errors were encountered: