-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor syntax highlighting to be blank by default
lets you decide in the theme
- Loading branch information
Showing
4 changed files
with
127 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package static | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
|
||
dom "github.com/PuerkitoBio/goquery" | ||
"github.com/alecthomas/chroma" | ||
"github.com/alecthomas/chroma/formatters/html" | ||
"github.com/alecthomas/chroma/lexers" | ||
) | ||
|
||
// blankStyle is a blank style. | ||
var blankStyle = chroma.MustNewStyle("blank", chroma.StyleEntries{}) | ||
|
||
// SyntaxHighlight returns a reader with HTML code prettified with chroma. | ||
func SyntaxHighlight(r io.Reader) io.ReadCloser { | ||
pr, pw := io.Pipe() | ||
|
||
go func() { | ||
doc, err := dom.NewDocumentFromReader(r) | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
|
||
formatter := html.New(html.WithClasses()) | ||
doc.Find("pre > code").Each(func(i int, s *dom.Selection) { | ||
lexer := detectLexer(s) | ||
code := s.Contents().Text() | ||
|
||
iterator, err := lexer.Tokenise(nil, code) | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
|
||
var buf bytes.Buffer | ||
err = formatter.Format(&buf, blankStyle, iterator) | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
|
||
// Parent() because chroma html is already in a <pre><code>. | ||
s.Parent().ReplaceWithHtml(buf.String()) | ||
}) | ||
|
||
html, err := doc.Html() | ||
if err != nil { | ||
pw.CloseWithError(err) | ||
return | ||
} | ||
|
||
pw.Write([]byte(html)) | ||
pw.Close() | ||
}() | ||
|
||
return pr | ||
} | ||
|
||
// detectLexer returns a chroma lexer based on the classname. | ||
func detectLexer(s *dom.Selection) chroma.Lexer { | ||
var lexer chroma.Lexer | ||
var lang string | ||
|
||
if classes, ok := s.Attr("class"); ok { | ||
for _, c := range strings.Split(classes, " ") { | ||
if strings.HasPrefix(c, "language-") { | ||
lang = strings.TrimPrefix(c, "language-") | ||
} | ||
} | ||
} | ||
|
||
if lang != "" { | ||
lexer = lexers.Get(lang) | ||
} | ||
|
||
if lexer == nil { | ||
lexer = lexers.Analyse(s.Contents().Text()) | ||
} | ||
|
||
if lexer == nil { | ||
lexer = lexers.Fallback | ||
} | ||
|
||
return chroma.Coalesce(lexer) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package static | ||
|
||
import ( | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestSyntaxHighlight(t *testing.T) { | ||
r := ioutil.NopCloser(strings.NewReader(` | ||
<pre><code> | ||
package main | ||
func main() { | ||
fmt.Println("foo") | ||
} | ||
</code></pre> | ||
<pre><code class="language-yaml"> | ||
foo: bar | ||
list: | ||
- 1 | ||
- 2 | ||
</code></pre> | ||
<pre><code> | ||
this is not even a lang | ||
</code></pre> | ||
`)) | ||
|
||
r = SyntaxHighlight(r) | ||
got, _ := ioutil.ReadAll(r) | ||
expect, _ := ioutil.ReadFile("testdata/code.html") | ||
if string(got) != string(expect) { | ||
t.Errorf("expected %s but got %s", string(expect), string(got)) | ||
// ioutil.WriteFile("testdata/code.html", got, 0644) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters