Skip to content

Commit

Permalink
Added: "absolute" argument for url helper. Changed: Url handling in c…
Browse files Browse the repository at this point in the history
…onfig. Updated: Promenade theme.
  • Loading branch information
kabukky committed Mar 17, 2015
1 parent 125de36 commit 425ff66
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"HttpHostAndPort":":8084","HttpsHostAndPort":":8085","HttpsUsage":"None","Url":"127.0.0.1:8084","HttpsUrl":"127.0.0.1:8085"}
{"HttpHostAndPort":":8084","HttpsHostAndPort":":8085","HttpsUsage":"None","Url":"http://127.0.0.1:8084","HttpsUrl":"https://127.0.0.1:8085"}
39 changes: 25 additions & 14 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (c *Configuration) save() error {
}

func (c *Configuration) load() error {
configWasChanged := false
data, err := ioutil.ReadFile(filenames.ConfigFilename)
if err != nil {
return err
Expand All @@ -59,24 +60,32 @@ func (c *Configuration) load() error {
return err
}
// Make sure the url is in the right format
if strings.HasPrefix(c.HttpsUrl, "http://") {
c.HttpsUrl = strings.Replace(c.HttpsUrl, "http://", "", 1)
} else if strings.HasPrefix(c.HttpsUrl, "https://") {
c.HttpsUrl = strings.Replace(c.HttpsUrl, "https://", "", 1)
}
// Make sure there is no trailing slash at the end of the url
if strings.HasSuffix(c.Url, "/") {
c.Url = c.Url[0 : len(c.Url)-1]
configWasChanged = true
}
if !strings.HasPrefix(c.Url, "http://") && !strings.HasPrefix(c.Url, "https://") {
c.Url = "http://" + c.Url
configWasChanged = true
}
// Make sure the https url is in the right format
// Make sure there is no trailing slash at the end of the https url
if strings.HasSuffix(c.HttpsUrl, "/") {
c.HttpsUrl = c.HttpsUrl[0 : len(c.HttpsUrl)-1]
configWasChanged = true
}
// Make sure the https url is in the right format
if strings.HasPrefix(c.HttpsUrl, "http://") {
c.HttpsUrl = strings.Replace(c.HttpsUrl, "http://", "", 1)
} else if strings.HasPrefix(c.HttpsUrl, "https://") {
c.HttpsUrl = strings.Replace(c.HttpsUrl, "https://", "", 1)
c.HttpsUrl = strings.Replace(c.HttpsUrl, "http://", "https://", 1)
configWasChanged = true
} else if !strings.HasPrefix(c.HttpsUrl, "https://") {
c.HttpsUrl = "https://" + c.HttpsUrl
configWasChanged = true
}
// Make sure there is no trailing slash at the end of the url
if strings.HasSuffix(c.HttpsUrl, "/") {
c.HttpsUrl = c.HttpsUrl[0 : len(c.HttpsUrl)-1]
configWasChanged = true
}
// Check if all fields are filled out
cReflected := reflect.ValueOf(*c)
Expand All @@ -86,11 +95,13 @@ func (c *Configuration) load() error {
return errors.New("Error: Configuration corrupted.")
}
}
// Save the changed config - NOT doing that for now.
//err = c.save()
//if err != nil {
// return err
//}
// Save the changed config
if configWasChanged {
err = c.save()
if err != nil {
return err
}
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion content/themes/promenade
Submodule promenade updated 2 files
+1 −1 page.hbs
+1 −1 post.hbs
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func httpsRedirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+configuration.Config.HttpsUrl+r.RequestURI, http.StatusMovedPermanently)
http.Redirect(w, r, configuration.Config.HttpsUrl+r.RequestURI, http.StatusMovedPermanently)
return
}

Expand Down
14 changes: 12 additions & 2 deletions templates/handlebars.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,24 @@ func post_classFunc(helper *Helper, values *structure.RequestData) []byte {
}

func urlFunc(helper *Helper, values *structure.RequestData) []byte {
var buffer bytes.Buffer
if len(helper.Arguments) != 0 {
for _, argument := range helper.Arguments {
// If the "absolute argument was used, prepend the blog url
if strings.HasPrefix(argument.Name, "absolute") {
if argument.Name[len("absolute"):] == "true" {
buffer.Write(values.Blog.Url)
}

}
}
}
if values.CurrentHelperContext == 1 { // post
var buffer bytes.Buffer
buffer.WriteString("/")
buffer.WriteString(values.Posts[values.CurrentPostIndex].Slug)
buffer.WriteString("/")
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
} else if values.CurrentHelperContext == 3 { // author
var buffer bytes.Buffer
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
Expand Down

0 comments on commit 425ff66

Please sign in to comment.