Source File
styleguide.go
Belonging Package
golang.org/x/pkgsite/internal/frontend
package frontend
import (
ghtml
)
func ( *Server) ( http.ResponseWriter, *http.Request, internal.DataSource) error {
:= .Context()
if !experiment.IsActive(, internal.ExperimentStyleGuide) {
return &serverError{status: http.StatusNotFound}
}
, := styleGuide(, .staticPath.String())
.basePage = .newBasePage(, "")
if != nil {
return
}
.servePage(, , "styleguide", )
return nil
}
type styleGuidePage struct {
basePage
Sections []*StyleSection
Outline []*Heading
}
func ( context.Context, string) ( *styleGuidePage, error) {
defer derrors.WrapStack(&, "styleGuide(%q)", )
, := markdownFiles()
if != nil {
return nil,
}
var []*StyleSection
for , := range {
, := styleSection(, )
if != nil {
return nil,
}
= append(, )
}
var []*Heading
for , := range {
= append(, .Outline...)
}
return &styleGuidePage{
Sections: ,
Outline: ,
}, nil
}
const (
= 10000
= 100
)
:= &extractTOC{ctx: }
:= goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(
parser.WithAutoHeadingID(),
parser.WithAttribute(),
parser.WithASTTransformers(
util.Prioritized(, ),
),
),
goldmark.WithRendererOptions(
renderer.WithNodeRenderers(
util.Prioritized(&guideRenderer{}, ),
),
ghtml.WithUnsafe(),
ghtml.WithXHTML(),
),
)
if := .Convert(, &); != nil {
return nil,
}
:= strings.TrimSuffix(filepath.Base(), ".md")
return &StyleSection{
ID: ,
Title: camelCase(),
Content: uncheckedconversions.HTMLFromStringKnownToSatisfyTypeContract(.String()),
Outline: .Headings,
}, nil
}
type guideRenderer struct {
ghtml.Config
}
func ( *guideRenderer) ( util.BufWriter, []byte, ast.Node) {
:= .Lines().Len()
for := 0; < ; ++ {
:= .Lines().At()
.Write(.Value())
}
}
func ( *guideRenderer) ( util.BufWriter, []byte, ast.Node) {
:= .Lines().Len()
for := 0; < ; ++ {
:= .Lines().At()
.Write([]byte(html.EscapeString(string(.Value()))))
}
}
func ( *guideRenderer) ( util.BufWriter, []byte, ast.Node, bool) (ast.WalkStatus, error) {
if ! {
return ast.WalkContinue, nil
}
:= .(*ast.FencedCodeBlock)
.WriteString("<span>\n")
.writeLines(, , )
.WriteString("</span>\n")
.WriteString("<pre class=\"StringifyElement-markup js-clipboard\">\n")
.writeEscapedLines(, , )
.WriteString("</pre>\n")
return ast.WalkContinue, nil
}
func ( *guideRenderer) ( renderer.NodeRendererFuncRegisterer) {
.Register(ast.KindFencedCodeBlock, .renderFencedCodeBlock)
}
func ( string) ([]string, error) {
var []string
:= filepath.Walk(, func( string, os.FileInfo, error) error {
if != nil {
return
}
if .IsDir() {
return nil
}
if , := filepath.Match("*.md", filepath.Base()); != nil {
return
} else if {
= append(, )
}
return nil
})
if != nil {
return nil,
}
return , nil
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |