mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
Reorganization of helpers. Centralized Url/Path logic. Fixed #175.
This commit is contained in:
51
helpers/general.go
Normal file
51
helpers/general.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://opensource.org/licenses/Simple-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func StripHTML(s string) string {
|
||||
output := ""
|
||||
|
||||
// Shortcut strings with no tags in them
|
||||
if !strings.ContainsAny(s, "<>") {
|
||||
output = s
|
||||
} else {
|
||||
s = strings.Replace(s, "\n", " ", -1)
|
||||
s = strings.Replace(s, "</p>", " \n", -1)
|
||||
s = strings.Replace(s, "<br>", " \n", -1)
|
||||
s = strings.Replace(s, "</br>", " \n", -1)
|
||||
|
||||
// Walk through the string removing all tags
|
||||
b := new(bytes.Buffer)
|
||||
inTag := false
|
||||
for _, r := range s {
|
||||
switch r {
|
||||
case '<':
|
||||
inTag = true
|
||||
case '>':
|
||||
inTag = false
|
||||
default:
|
||||
if !inTag {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
}
|
||||
output = b.String()
|
||||
}
|
||||
return output
|
||||
}
|
76
helpers/helpers_test.go
Normal file
76
helpers/helpers_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPretty(t *testing.T) {
|
||||
assert.Equal(t, PrettifyPath("/section/name.html"), "/section/name/index.html")
|
||||
assert.Equal(t, PrettifyPath("/section/sub/name.html"), "/section/sub/name/index.html")
|
||||
assert.Equal(t, PrettifyPath("/section/name/"), "/section/name/index.html")
|
||||
assert.Equal(t, PrettifyPath("/section/name/index.html"), "/section/name/index.html")
|
||||
assert.Equal(t, PrettifyPath("/index.html"), "/index.html")
|
||||
assert.Equal(t, PrettifyPath("/name.xml"), "/name/index.xml")
|
||||
assert.Equal(t, PrettifyPath("/"), "/")
|
||||
assert.Equal(t, PrettifyPath(""), "/")
|
||||
assert.Equal(t, PrettifyUrl("/section/name.html"), "/section/name")
|
||||
assert.Equal(t, PrettifyUrl("/section/sub/name.html"), "/section/sub/name")
|
||||
assert.Equal(t, PrettifyUrl("/section/name/"), "/section/name")
|
||||
assert.Equal(t, PrettifyUrl("/section/name/index.html"), "/section/name")
|
||||
assert.Equal(t, PrettifyUrl("/index.html"), "/")
|
||||
assert.Equal(t, PrettifyUrl("/name.xml"), "/name/index.xml")
|
||||
assert.Equal(t, PrettifyUrl("/"), "/")
|
||||
assert.Equal(t, PrettifyUrl(""), "/")
|
||||
}
|
||||
|
||||
func TestUgly(t *testing.T) {
|
||||
assert.Equal(t, Uglify("/section/name.html"), "/section/name.html")
|
||||
assert.Equal(t, Uglify("/section/sub/name.html"), "/section/sub/name.html")
|
||||
assert.Equal(t, Uglify("/section/name/"), "/section/name.html")
|
||||
assert.Equal(t, Uglify("/section/name/index.html"), "/section/name.html")
|
||||
assert.Equal(t, Uglify("/index.html"), "/index.html")
|
||||
assert.Equal(t, Uglify("/name.xml"), "/name.xml")
|
||||
assert.Equal(t, Uglify("/"), "/")
|
||||
assert.Equal(t, Uglify(""), "/")
|
||||
}
|
||||
|
||||
func TestMakePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{" foo bar ", "foo-bar"},
|
||||
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
||||
{"foo,bar:foo%bar", "foobarfoobar"},
|
||||
{"foo/bar.html", "foo/bar.html"},
|
||||
{"трям/трям", "трям/трям"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
output := MakePath(test.input)
|
||||
if output != test.expected {
|
||||
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUrlize(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{" foo bar ", "foo-bar"},
|
||||
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
||||
{"foo,bar:foo%bar", "foobarfoobar"},
|
||||
{"foo/bar.html", "foo/bar.html"},
|
||||
{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
output := Urlize(test.input)
|
||||
if output != test.expected {
|
||||
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
||||
}
|
||||
}
|
||||
}
|
120
helpers/path.go
Normal file
120
helpers/path.go
Normal file
@@ -0,0 +1,120 @@
|
||||
// Copyright © 2014 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://opensource.org/licenses/Simple-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]")
|
||||
|
||||
// Take a string with any characters and replace it so the string could be used in a path.
|
||||
// E.g. Social Media -> social-media
|
||||
func MakePath(s string) string {
|
||||
return UnicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1)))
|
||||
}
|
||||
|
||||
func Sanitize(s string) string {
|
||||
return sanitizeRegexp.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
func UnicodeSanitize(s string) string {
|
||||
source := []rune(s)
|
||||
target := make([]rune, 0, len(source))
|
||||
|
||||
for _, r := range source {
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '.' || r == '/' || r == '_' || r == '-' {
|
||||
target = append(target, r)
|
||||
}
|
||||
}
|
||||
|
||||
return string(target)
|
||||
}
|
||||
|
||||
func ReplaceExtension(path string, newExt string) string {
|
||||
f, _ := FileAndExt(path)
|
||||
return f + "." + newExt
|
||||
}
|
||||
|
||||
// Check if Exists && is Directory
|
||||
func DirExists(path string) (bool, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err == nil && fi.IsDir() {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// Check if File / Directory Exists
|
||||
func Exists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
func FileAndExt(in string) (name string, ext string) {
|
||||
ext = path.Ext(in)
|
||||
base := path.Base(in)
|
||||
|
||||
if strings.Contains(base, ".") {
|
||||
name = base[:strings.LastIndex(base, ".")]
|
||||
} else {
|
||||
name = in
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func PathPrep(ugly bool, in string) string {
|
||||
if ugly {
|
||||
return Uglify(in)
|
||||
} else {
|
||||
return PrettifyPath(in)
|
||||
}
|
||||
}
|
||||
|
||||
// /section/name.html -> /section/name/index.html
|
||||
// /section/name/ -> /section/name/index.html
|
||||
// /section/name/index.html -> /section/name/index.html
|
||||
func PrettifyPath(in string) string {
|
||||
if path.Ext(in) == "" {
|
||||
// /section/name/ -> /section/name/index.html
|
||||
if len(in) < 2 {
|
||||
return "/"
|
||||
}
|
||||
return path.Join(path.Clean(in), "index.html")
|
||||
} else {
|
||||
name, ext := FileAndExt(in)
|
||||
if name == "index" {
|
||||
// /section/name/index.html -> /section/name/index.html
|
||||
return path.Clean(in)
|
||||
} else {
|
||||
// /section/name.html -> /section/name/index.html
|
||||
return path.Join(path.Dir(in), name, "index"+ext)
|
||||
}
|
||||
}
|
||||
return in
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://opensource.org/licenses/Simple-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var sanitizeRegexp = regexp.MustCompile("[^a-zA-Z0-9./_-]")
|
||||
|
||||
func MakePath(s string) string {
|
||||
return unicodeSanitize(strings.ToLower(strings.Replace(strings.TrimSpace(s), " ", "-", -1)))
|
||||
}
|
||||
|
||||
func Urlize(uri string) string {
|
||||
sanitized := MakePath(uri)
|
||||
|
||||
// escape unicode letters
|
||||
parsedUri, err := url.Parse(sanitized)
|
||||
if err != nil {
|
||||
// if net/url can not parse URL it's meaning Sanitize works incorrect
|
||||
panic(err)
|
||||
}
|
||||
return parsedUri.String()
|
||||
}
|
||||
|
||||
func Sanitize(s string) string {
|
||||
return sanitizeRegexp.ReplaceAllString(s, "")
|
||||
}
|
||||
|
||||
func unicodeSanitize(s string) string {
|
||||
source := []rune(s)
|
||||
target := make([]rune, 0, len(source))
|
||||
|
||||
for _, r := range source {
|
||||
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '.' || r == '/' || r == '_' || r == '-' {
|
||||
target = append(target, r)
|
||||
}
|
||||
}
|
||||
|
||||
return string(target)
|
||||
}
|
@@ -1,45 +0,0 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMakePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{" foo bar ", "foo-bar"},
|
||||
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
||||
{"foo,bar:foo%bar", "foobarfoobar"},
|
||||
{"foo/bar.html", "foo/bar.html"},
|
||||
{"трям/трям", "трям/трям"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
output := MakePath(test.input)
|
||||
if output != test.expected {
|
||||
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUrlize(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{" foo bar ", "foo-bar"},
|
||||
{"foo.bar/foo_bar-foo", "foo.bar/foo_bar-foo"},
|
||||
{"foo,bar:foo%bar", "foobarfoobar"},
|
||||
{"foo/bar.html", "foo/bar.html"},
|
||||
{"трям/трям", "%D1%82%D1%80%D1%8F%D0%BC/%D1%82%D1%80%D1%8F%D0%BC"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
output := Urlize(test.input)
|
||||
if output != test.expected {
|
||||
t.Errorf("Expected %#v, got %#v\n", test.expected, output)
|
||||
}
|
||||
}
|
||||
}
|
110
helpers/url.go
Normal file
110
helpers/url.go
Normal file
@@ -0,0 +1,110 @@
|
||||
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
||||
//
|
||||
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
// http://opensource.org/licenses/Simple-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path"
|
||||
)
|
||||
|
||||
var _ = fmt.Println
|
||||
|
||||
// Similar to MakePath, but with Unicode handling
|
||||
// Example:
|
||||
// uri: Vim (text editor)
|
||||
// urlize: vim-text-editor
|
||||
func Urlize(uri string) string {
|
||||
sanitized := MakePath(uri)
|
||||
|
||||
// escape unicode letters
|
||||
parsedUri, err := url.Parse(sanitized)
|
||||
if err != nil {
|
||||
// if net/url can not parse URL it's meaning Sanitize works incorrect
|
||||
panic(err)
|
||||
}
|
||||
x := parsedUri.String()
|
||||
return x
|
||||
}
|
||||
|
||||
// Combines a base with a path
|
||||
// Example
|
||||
// base: http://spf13.com/
|
||||
// path: post/how-i-blog
|
||||
// result: http://spf13.com/post/how-i-blog
|
||||
func MakePermalink(host, plink string) *url.URL {
|
||||
|
||||
base, err := url.Parse(host)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
path, err := url.Parse(plink)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return base.ResolveReference(path)
|
||||
}
|
||||
|
||||
func UrlPrep(ugly bool, in string) string {
|
||||
if ugly {
|
||||
return Uglify(in)
|
||||
} else {
|
||||
return PrettifyUrl(in)
|
||||
}
|
||||
}
|
||||
|
||||
// Don't Return /index.html portion.
|
||||
func PrettifyUrl(in string) string {
|
||||
x := PrettifyPath(in)
|
||||
|
||||
if path.Base(x) == "index.html" {
|
||||
return path.Dir(x)
|
||||
}
|
||||
|
||||
if in == "" {
|
||||
return "/"
|
||||
}
|
||||
|
||||
return x
|
||||
}
|
||||
|
||||
// /section/name/index.html -> /section/name.html
|
||||
// /section/name/ -> /section/name.html
|
||||
// /section/name.html -> /section/name.html
|
||||
func Uglify(in string) string {
|
||||
if path.Ext(in) == "" {
|
||||
if len(in) < 2 {
|
||||
return "/"
|
||||
}
|
||||
// /section/name/ -> /section/name.html
|
||||
return path.Clean(in) + ".html"
|
||||
} else {
|
||||
name, ext := FileAndExt(in)
|
||||
if name == "index" {
|
||||
// /section/name/index.html -> /section/name.html
|
||||
d := path.Dir(in)
|
||||
if len(d) > 1 {
|
||||
return d + ext
|
||||
} else {
|
||||
return in
|
||||
}
|
||||
} else {
|
||||
// /section/name.html -> /section/name.html
|
||||
return path.Clean(in)
|
||||
}
|
||||
}
|
||||
|
||||
return in
|
||||
}
|
Reference in New Issue
Block a user