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
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 file: scheme or a http: scheme, or even https:.
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 /, which is OK on a webserver, but rubbish for your use-case. I had hoped that _not a single_ intra-content URL would start with /; that is the acceptance criterion for a portable bundle of content.
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 file: scheme play nicely.
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.
Plan to convert it to python on some later date.
Known issues:
'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 irc:// links that got turned into #ZgotmplZ. This was fixed by the addition of the safeUrl template function in commit 724cc0ddff3427a37b1fa4367880fce23bb4f1f8 on 19 Jan 2015, now in v0.13-DEV. This new feature is documented around http://gohugo.io/templates/functions/#toc_12 (scroll down a bit to see it).
So, @JeremyBYU, with Hugo v0.13-DEV, you may now use a baseurl = "file:///path/to/your/website/" with uglyurls = true and have the links work by modifying your templates to use, for example, <a href="{{ .Permalink | safeUrl }}> rather than <a href="{{ .Permalink }}">.
Please let us know how it works out for you!
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 took a brief look at it some weeks ago, and thought we could implement something very similar to http://golang.org/pkg/path/filepath/#Rel, but for URL rather than file names. But I was soon busy and haven't had time to follow up yet. (I am still a Go learner.)
I agree that using safeUrl was an ugly hack; I was merely responding to the <a href="#ZgotmplZ"> problem that @JeremyBYU reported in the original post. Though an ugly hack, it worksâ„¢ for those who need it until proper relative URLs support is available in Hugo.
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! :wink:
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.
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:
baseURL = "file:///path/to/networkfolder/doc/public/"
will not result in a working site whether RelativeURLs is set to true or false. If true, themes go missing completely, hugo server doesn't work (it changes localhost:1313 to localhost:1313/path/...), etc.
Can this issue be reopened, or closed as wontfix?
Update: after a couple of hours of digging I found the answer: relativeURLs = "true" only works in combination with uglyURLs = "true". See https://github.com/gohugoio/hugo/issues/4642#issuecomment-382520968 for more details.
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:
- Do not set
baseURL- Set
relativeURLs = "true"- Set
uglyURLs = "true"
But it doesn't work for me.
Couldn't we simply create a hugo parameter to build "for local" while taking care of all these "not for local" problems, while not depending on baseURL, relativeURLs, uglyURLs, canonifyURLs (and so on)? hugo --local for example.
Most helpful comment
I tried:
But it doesn't work for me.
Couldn't we simply create a
hugoparameter to build "for local" while taking care of all these "not for local" problems, while not depending onbaseURL,relativeURLs,uglyURLs,canonifyURLs(and so on)?hugo --localfor example.