mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-28 22:19:59 +02:00
@@ -198,6 +198,7 @@ func newHighlighting(cfg highlight.Config) goldmark.Extender {
|
||||
|
||||
e := hl.NewHighlighting(
|
||||
hl.WithStyle(cfg.Style),
|
||||
hl.WithGuessLanguage(cfg.GuessSyntax),
|
||||
hl.WithCodeBlockOptions(highlight.GetCodeBlockOptions()),
|
||||
hl.WithFormatOptions(
|
||||
cfg.ToHTMLOptions()...,
|
||||
|
@@ -224,4 +224,25 @@ LINE5
|
||||
result = convertForConfig(c, cfg, lines, "bash {linenos=table}")
|
||||
c.Assert(result, qt.Contains, "<span class=\"lnt\">1\n</span>")
|
||||
})
|
||||
|
||||
c.Run("No language", func(c *qt.C) {
|
||||
cfg := highlight.DefaultConfig
|
||||
cfg.NoClasses = false
|
||||
cfg.LineNos = true
|
||||
cfg.LineNumbersInTable = false
|
||||
|
||||
result := convertForConfig(c, cfg, lines, "")
|
||||
c.Assert(result, qt.Contains, "<pre><code>LINE1\n")
|
||||
})
|
||||
|
||||
c.Run("No language, guess syntax", func(c *qt.C) {
|
||||
cfg := highlight.DefaultConfig
|
||||
cfg.NoClasses = false
|
||||
cfg.GuessSyntax = true
|
||||
cfg.LineNos = true
|
||||
cfg.LineNumbersInTable = false
|
||||
|
||||
result := convertForConfig(c, cfg, lines, "")
|
||||
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n<")
|
||||
})
|
||||
}
|
||||
|
@@ -58,6 +58,8 @@ type Config struct {
|
||||
|
||||
// TabWidth sets the number of characters for a tab. Defaults to 4.
|
||||
TabWidth int
|
||||
|
||||
GuessSyntax bool
|
||||
}
|
||||
|
||||
func (cfg Config) ToHTMLOptions() []html.Option {
|
||||
@@ -104,6 +106,10 @@ func ApplyLegacyConfig(cfg config.Provider, conf *Config) error {
|
||||
conf.CodeFences = cfg.GetBool("pygmentsCodeFences")
|
||||
}
|
||||
|
||||
if conf.GuessSyntax == DefaultConfig.GuessSyntax && cfg.IsSet("pygmentsCodefencesGuessSyntax") {
|
||||
conf.GuessSyntax = cfg.GetBool("pygmentsCodefencesGuessSyntax")
|
||||
}
|
||||
|
||||
if cfg.IsSet("pygmentsOptions") {
|
||||
if err := applyOptionsFromString(cfg.GetString("pygmentsOptions"), conf); err != nil {
|
||||
return err
|
||||
|
@@ -52,6 +52,14 @@ func highlight(code, lang string, cfg Config) (string, error) {
|
||||
lexer = lexers.Get(lang)
|
||||
}
|
||||
|
||||
if lexer == nil && cfg.GuessSyntax {
|
||||
lexer = lexers.Analyse(code)
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
lang = strings.ToLower(lexer.Config().Name)
|
||||
}
|
||||
|
||||
if lexer == nil {
|
||||
wrapper := getPreWrapper(lang)
|
||||
fmt.Fprint(w, wrapper.Start(true, ""))
|
||||
|
@@ -84,4 +84,26 @@ LINE5
|
||||
result, _ = h.Highlight(lines, "bash", "linenos=table")
|
||||
c.Assert(result, qt.Contains, "<span class=\"lnt\">1\n</span>")
|
||||
})
|
||||
|
||||
c.Run("No language", func(c *qt.C) {
|
||||
cfg := DefaultConfig
|
||||
cfg.NoClasses = false
|
||||
cfg.LineNos = true
|
||||
h := New(cfg)
|
||||
|
||||
result, _ := h.Highlight(lines, "", "")
|
||||
c.Assert(result, qt.Equals, "<pre><code>LINE1\nLINE2\nLINE3\nLINE4\nLINE5\n</code></pre>")
|
||||
})
|
||||
|
||||
c.Run("No language, guess syntax", func(c *qt.C) {
|
||||
cfg := DefaultConfig
|
||||
cfg.NoClasses = false
|
||||
cfg.GuessSyntax = true
|
||||
cfg.LineNos = true
|
||||
cfg.LineNumbersInTable = false
|
||||
h := New(cfg)
|
||||
|
||||
result, _ := h.Highlight(lines, "", "")
|
||||
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span>LINE2\n<")
|
||||
})
|
||||
}
|
||||
|
@@ -55,7 +55,7 @@ func TestConfig(t *testing.T) {
|
||||
v.Set("footnoteAnchorPrefix", "myprefix")
|
||||
v.Set("footnoteReturnLinkContents", "myreturn")
|
||||
v.Set("pygmentsStyle", "hugo")
|
||||
|
||||
v.Set("pygmentsCodefencesGuessSyntax", true)
|
||||
conf, err := Decode(v)
|
||||
|
||||
c.Assert(err, qt.IsNil)
|
||||
@@ -64,6 +64,7 @@ func TestConfig(t *testing.T) {
|
||||
c.Assert(conf.BlackFriday.FootnoteReturnLinkContents, qt.Equals, "myreturn")
|
||||
c.Assert(conf.Highlight.Style, qt.Equals, "hugo")
|
||||
c.Assert(conf.Highlight.CodeFences, qt.Equals, true)
|
||||
c.Assert(conf.Highlight.GuessSyntax, qt.Equals, true)
|
||||
})
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user