summaryrefslogtreecommitdiffstats
path: root/markup/highlight
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-12-02 08:31:23 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-12-02 14:12:23 +0100
commit40a092b0687d44ecb53ef1fd53001a6299345780 (patch)
tree2a5047d0affd152da90a8e3105fa12ad470989b2 /markup/highlight
parentd534ce9424c952800dfb26c2faff2d47e9597cad (diff)
markup: Reimplement pygmentsCodefencesGuessSyntax
Fixes #6565
Diffstat (limited to 'markup/highlight')
-rw-r--r--markup/highlight/config.go6
-rw-r--r--markup/highlight/highlight.go8
-rw-r--r--markup/highlight/highlight_test.go22
3 files changed, 36 insertions, 0 deletions
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<")
+ })
}