Documentation ¶
Overview ¶
Copyright 2013 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. Package slug transforms strings into a normalized form well suited for use in URLs.
Index ¶
- Constants
- Variables
- func CauseError(article *Article) error
- func ChromaCode(article *Article) error
- func ExtractFrontMatter(article *Article) (err error)
- func ExtractPreview(article *Article) (err error)
- func ExtractTags(article *Article) (err error)
- func ExtractTitle(article *Article) (err error)
- func HumanizeBytes(size int) string
- func IsSlugAscii(s string) bool
- func MarkdownToHTML(article *Article) (err error)
- func Noop(article *Article) error
- func ParseFilename(article *Article) (err error)
- func PygmentizeCode(article *Article) error
- func Slug(s string) string
- func SlugAscii(s string) string
- func Slugify(input string) string
- func SplitFilename(input string) (string, string)
- func UnSnakeCase(input string) string
- type Article
- func (article Article) ParseDuration() time.Duration
- func (article Article) RenderWith(tmpl *template.Template) ([]byte, error)
- func (article Article) SaveAs() string
- func (article Article) String() string
- func (article *Article) Transform(steps ...Transformer) error
- func (article Article) URL() string
- type Articles
- type Attrs
- type Boondoggle
- type ByDate
- type ByTitle
- type Links
- type Section
- type Templates
- type Transformer
- type UseDefault
- type UseSlugs
Constants ¶
const ( MarkdownExt = ".md" // MarkdownExt is the common markdown file ending HTMLExt = ".html" )
const ( FrontMatterBlock = "---" CodeFence = "```" NewLine = "\n" Space = " " TagsPrefix = "<!-- tags:" // TODO Rather unfriendly PreviewPrefix = "<!-- preview:" // TODO Rather unfriendly ClosingComment = "-->" TitleAtx = "#" TitleSetext = "=" )
All tokens that will be used by ad-hoc transformations
const ISO8601Date = "2006-01-02"
Variables ¶
var DefaultProcessor = []Transformer{ ParseFilename, ExtractFrontMatter, ExtractTitle, ExtractTags, ExtractPreview, ChromaCode, MarkdownToHTML, TruncatedTagPreview(200), }
var ExampleArticleTemplate = template.Must(template.New("article").Parse(`<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
</head>
<body>
<h1>{{ .Title }}</h1>
<h4>{{ .Date.Format "Monday, January 2, 2006" }}<h4>
{{ .HTML }}
</body>
</html>
`))
Example Templates
var ExampleIndexTemplate = template.Must(template.New("index").Parse(`<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
</head>
<body>
{{ range $article := .Articles }}
<article>
<h1>{{ $article.Title }}</h1>
</article>
{{ end }}
</body>
}
</html>
`))
var ExampleTagsTemplate = template.Must(template.New("tags").Parse(`<!DOCTYPE html>
<html>
<head>
<title>Tags</title>
</head>
<body>
<ul>
{{ range $tag := .Tags }}
<li>{{ $tag }}</li>
{{ end }}
</ul>
</body>
</html>
`))
Functions ¶
func CauseError ¶
CauseError is an example Transformer that returns an error
func ChromaCode ¶
func ExtractFrontMatter ¶
ExtractFrontMatter will parse and remove any Front Matter (https://jekyllrb.com/docs/frontmatter/) metadata tags from the raw markdown. For front matter to be parsed, the first line of the markdown must start with "---"
func ExtractPreview ¶
ExtractPreview will parse and remove a preview from raw markdown. The preview markdown will be converted to HTML after parsing.
func ExtractTags ¶
ExtractTags will parse and remove the tags from the raw markdown. The Tags will be converted to slugs after parsing.
func ExtractTitle ¶
ExtractTitle will parse and remove an atx or setext H1 title from the first line (and second if setext) of the markdown file. TODO ExtractTitle will add a newline - it shouldn'y
func HumanizeBytes ¶
func IsSlugAscii ¶
IsSlugAscii returns true only if SlugAscii(s) == s.
func MarkdownToHTML ¶
MarkdownToHTML will convert the raw markdown bytes to an HTML template.
func ParseFilename ¶
ParseFilename will parse the filename for the slug and date. The filename must be in the format YYYY-MM-DD_title.md
func PygmentizeCode ¶
func Slug ¶
Slug replaces each run of characters which are not unicode letters or numbers with a single hyphen, except for leading or trailing runs. Letters will be stripped of diacritical marks and lowercased. Letter or number codepoints that do not have combining marks or a lower-cased variant will be passed through unaltered.
func SlugAscii ¶
SlugAscii is identical to Slug, except that runs of one or more unicode letters or numbers that still fall outside the ASCII range will have their UTF-8 representation hex encoded and delimited by hyphens. As with Slug, in no case will hyphens appear at either end of the returned string.
func SplitFilename ¶
Split a date and title from the given input
func UnSnakeCase ¶
Types ¶
type Article ¶
type Article struct { // Content Title string Slug string Date time.Time Subtitle string Byline string HTML template.HTML // Meta Preview template.HTML WordCount uint64 TableOfContents Section LinesOfCode uint64 Tags []string Metadata Attrs Now time.Time Links Links // Parsing ParseStart time.Time ParseEnd time.Time Filename string Raw []byte // The entire raw file }
Article is a single article
func (Article) ParseDuration ¶
func (Article) RenderWith ¶
RenderWith renders the Article with the given Template
func (*Article) Transform ¶
func (article *Article) Transform(steps ...Transformer) error
Transform modifies the given Article, for instance by converting its markdown to HTML, or performing syntax highlighting
type Articles ¶
type Articles []Article
Articles is a slice of articles
func (Articles) SortMostRecentArticlesFirst ¶
func (a Articles) SortMostRecentArticlesFirst()
SortMostRecentArticlesFirst will sort the articles by Date
type Boondoggle ¶
type Boondoggle struct { Links Links // Writes URLs Articles Articles ByTitle map[string]Article ByTag map[string]Articles Metadata Attrs BuildTime time.Time }
Boondoggle builds .html files from a directory of markdown files.
func New ¶
func New() *Boondoggle
New creates a new Boondoggle. The New method does not need to be used directly - use ParseDirectory instead
func ParseDirectory ¶
func ParseDirectory(path string, steps ...Transformer) (*Boondoggle, error)
ParseDirectory will parse all markdown files in the given directory
func (*Boondoggle) ReadDirectory ¶
func (bd *Boondoggle) ReadDirectory(path string) error
func (Boondoggle) Tags ¶
func (bd Boondoggle) Tags() (tags []string)
Tags returns tags in alphabetical order
type Templates ¶
func ParseTemplates ¶
type Transformer ¶
Transformer is the type signature of functions that modify Article types
func Preview ¶
func Preview(minLength int) Transformer
func TruncatedTagPreview ¶
func TruncatedTagPreview(minLength int) Transformer
func (Transformer) String ¶
func (t Transformer) String() string
String returns the name of the transformer function
type UseDefault ¶
type UseDefault struct{}
func (UseDefault) ForArticle ¶
func (links UseDefault) ForArticle(article Article) string
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
Package syntax contains components to perform syntax highlighting
|
Package syntax contains components to perform syntax highlighting |