Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/markup
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-12-02 10:31:23 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-12-02 16:12:23 +0300
commit40a092b0687d44ecb53ef1fd53001a6299345780 (patch)
tree2a5047d0affd152da90a8e3105fa12ad470989b2 /markup
parentd534ce9424c952800dfb26c2faff2d47e9597cad (diff)
markup: Reimplement pygmentsCodefencesGuessSyntax
Fixes #6565
Diffstat (limited to 'markup')
-rw-r--r--markup/goldmark/convert.go1
-rw-r--r--markup/goldmark/convert_test.go21
-rw-r--r--markup/highlight/config.go6
-rw-r--r--markup/highlight/highlight.go8
-rw-r--r--markup/highlight/highlight_test.go22
-rw-r--r--markup/markup_config/config_test.go3
6 files changed, 60 insertions, 1 deletions
diff --git a/markup/goldmark/convert.go b/markup/goldmark/convert.go
index 167286831..15b0f0d77 100644
--- a/markup/goldmark/convert.go
+++ b/markup/goldmark/convert.go
@@ -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()...,
diff --git a/markup/goldmark/convert_test.go b/markup/goldmark/convert_test.go
index a23976fd6..b6816d2e5 100644
--- a/markup/goldmark/convert_test.go
+++ b/markup/goldmark/convert_test.go
@@ -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<")
+ })
}
diff --git a/markup/highlight/config.go b/markup/highlight/config.go
index 56e38fd85..3f31e65ea 100644
--- a/markup/highlight/config.go
+++ b/markup/highlight/config.go
@@ -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
diff --git a/markup/highlight/highlight.go b/markup/highlight/highlight.go
index 322bde1ef..9e26aaf84 100644
--- a/markup/highlight/highlight.go
+++ b/markup/highlight/highlight.go
@@ -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, ""))
diff --git a/markup/highlight/highlight_test.go b/markup/highlight/highlight_test.go
index 58bd9c119..6da292489 100644
--- a/markup/highlight/highlight_test.go
+++ b/markup/highlight/highlight_test.go
@@ -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<")
+ })
}
diff --git a/markup/markup_config/config_test.go b/markup/markup_config/config_test.go
index 726d1146b..22f0ab1d4 100644
--- a/markup/markup_config/config_test.go
+++ b/markup/markup_config/config_test.go
@@ -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)
})
}