1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-21 21:17:50 +02:00

Correct HTML output + add R/S.

Fixes #1, #2, #3.
This commit is contained in:
Alec Thomas 2017-09-19 10:29:24 +10:00
parent d4a6811691
commit fbc889aad6
4 changed files with 116 additions and 59 deletions

View File

@ -27,22 +27,7 @@ translaters for Pygments lexers and styles.
## Supported languages ## Supported languages
ABNF, ActionScript, ActionScript 3, Ada, Angular2, ANTLR, ApacheConf, APL, ABNF, ActionScript, ActionScript, Ada, Angular2, ANTLR, ApacheConf, APL, AppleScript, Awk, Bash, Batchfile, BlitzBasic, BNF, Brainfuck, C, Cap'n, Ceylon, CFEngine3, ChaiScript, Python, Cheetah, Common, Clojure, CMake, COBOL, CoffeeScript, cfstatement, Coq, C++, Crystal, CSS, Cython, Dart, Diff, Django/Jinja, Docker, DTD, EBNF, Elixir, Elm, EmacsLisp, Erlang, Factor, Fish, Forth, Fortran, FSharp, GAS, Genshi, GLSL, Gnuplot, Go, Groovy, Handlebars, Haskell, Haxe, JavaScript, HTML, Hy, Idris, INI, Io, Java, JSON, Julia, Kotlin, Lighttpd, LLVM, Lua, Base, Mako, markdown, Perl, Mason, Mathematica, Modula-2, Myghty, MySQL, NASM, Newspeak, Nginx, Nimrod, OCaml, Octave, PacmanConf, PHP, Pig, PkgConfig, PL/pgSQL, PostgreSQL, PostScript, POVRay, PowerShell, Prolog, Protocol, Puppet, Python, QBasic, S, Racket, Ragel, reg, Rexx, Ruby, Rust, Sass, Scala, Scheme, Scilab, Smalltalk, Smarty, Snobol, SPARQL, SQL, SquidConf, Swift, TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, Thrift, Transact-SQL, Turtle, Twig, TypeScript, TypoScriptHtmlData, TypoScriptCssData, TypoScript, verilog, vhdl, VimL, XML, Xorg
AppleScript, Awk, Bash, Batchfile, BlitzBasic, BNF, Brainfuck, C, Cap'n
Proto, Ceylon, CFEngine3, ChaiScript, Python, Cheetah, Common Lisp, Clojure,
COBOL, CoffeeScript, cfstatement, Coq, C++, Crystal, CSS, Cython, Dart, Diff,
Django/Jinja, Docker, DTD, EBNF, Elixir, Elm, EmacsLisp, Erlang, Factor, Fish,
Forth, Fortran, FSharp, GAS, Genshi Text, GLSL, Gnuplot, Go, Groovy,
Handlebars, Haskell, Haxe, JavaScript, HTML, Hy, Idris, INI, Io, Java, JSON,
Julia, Kotlin, Lighttpd configuration file, LLVM, Lua, Base Makefile, Mako,
markdown, Perl, Mason, Mathematica, Modula-2, Myghty, MySQL, NASM, Newspeak,
Nginx configuration file, Nimrod, OCaml, Octave, PacmanConf, PHP, Pig,
PkgConfig, PL/pgSQL, PostgreSQL SQL dialect, PostScript, POVRay, PowerShell,
Prolog, Protocol Buffer, Puppet, Python 3, QBasic, Racket, Ragel, reg, Rexx,
Ruby, Rust, Sass, Scala, Scheme, Scilab, Smalltalk, Smarty, Snobol, SPARQL,
SQL, SquidConf, Swift, TASM, Tcl, Tcsh, Termcap, Terminfo, Terraform, Thrift,
Transact-SQL, Turtle, Twig, TypeScript, TypoScript, verilog, vhdl, VimL, XML,
Xorg
_I will attempt to keep this section up to date, but an authoritative list can be _I will attempt to keep this section up to date, but an authoritative list can be
displayed with `chroma --list`._ displayed with `chroma --list`._

View File

@ -76,17 +76,19 @@ command, for Go.
if *htmlPrefixFlag != "" { if *htmlPrefixFlag != "" {
options = append(options, html.ClassPrefix(*htmlPrefixFlag)) options = append(options, html.ClassPrefix(*htmlPrefixFlag))
} }
if !*htmlOnlyFlag {
options = append(options, html.Standalone()) // Dump styles.
}
if !*htmlInlineStyleFlag {
options = append(options, html.WithClasses())
}
if *htmlStylesFlag { if *htmlStylesFlag {
formatter := html.New(html.WithClasses()) formatter := html.New(html.WithClasses())
formatter.WriteCSS(w, styles.Get(*styleFlag)) formatter.WriteCSS(w, styles.Get(*styleFlag))
return return
} }
if !*htmlInlineStyleFlag {
options = append(options, html.WithClasses())
}
if !*htmlOnlyFlag {
options = append(options, html.Standalone())
}
formatters.Register("html", html.New(options...)) formatters.Register("html", html.New(options...))
} }
writer := getWriter(w) writer := getWriter(w)

View File

@ -62,7 +62,9 @@ func (h *HTMLFormatter) formatWithoutClasses(w io.Writer, style *chroma.Style) (
fmt.Fprint(w, "</body>\n") fmt.Fprint(w, "</body>\n")
fmt.Fprint(w, "</html>\n") fmt.Fprint(w, "</html>\n")
} }
} else { return
}
html := html.EscapeString(token.String()) html := html.EscapeString(token.String())
style := classes[token.Type] style := classes[token.Type]
if style == "" { if style == "" {
@ -76,7 +78,6 @@ func (h *HTMLFormatter) formatWithoutClasses(w io.Writer, style *chroma.Style) (
} else { } else {
fmt.Fprintf(w, "<span style=\"%s\">%s</span>", style, html) fmt.Fprintf(w, "<span style=\"%s\">%s</span>", style, html)
} }
}
}, nil }, nil
} }
@ -100,22 +101,23 @@ func (h *HTMLFormatter) formatWithClasses(w io.Writer, style *chroma.Style) (fun
classes := h.typeStyles(style) classes := h.typeStyles(style)
if h.standalone { if h.standalone {
fmt.Fprint(w, "<html>\n") fmt.Fprint(w, "<html>\n")
}
fmt.Fprint(w, "<style type=\"text/css\">\n") fmt.Fprint(w, "<style type=\"text/css\">\n")
h.WriteCSS(w, style) h.WriteCSS(w, style)
if h.standalone {
fmt.Fprintf(w, "body { %s; }\n", classes[chroma.Background]) fmt.Fprintf(w, "body { %s; }\n", classes[chroma.Background])
}
fmt.Fprint(w, "</style>\n") fmt.Fprint(w, "</style>\n")
if h.standalone {
fmt.Fprint(w, "<body>\n") fmt.Fprint(w, "<body>\n")
} }
fmt.Fprint(w, "<pre class=\"chroma\">\n") fmt.Fprint(w, "<pre class=\"chroma\">\n")
return func(token *chroma.Token) { return func(token *chroma.Token) {
if h.standalone && token.Type == chroma.EOF { if token.Type == chroma.EOF {
fmt.Fprint(w, "</pre>\n")
if h.standalone {
fmt.Fprint(w, "</body>\n") fmt.Fprint(w, "</body>\n")
fmt.Fprint(w, "</html>\n") fmt.Fprint(w, "</html>\n")
} else { }
return
}
tt := token.Type tt := token.Type
class := classes[tt] class := classes[tt]
if class == "" { if class == "" {
@ -132,7 +134,6 @@ func (h *HTMLFormatter) formatWithClasses(w io.Writer, style *chroma.Style) (fun
html := html.EscapeString(token.String()) html := html.EscapeString(token.String())
fmt.Fprintf(w, "<span class=\"%ss%x\">%s</span>", h.prefix, int(tt), html) fmt.Fprintf(w, "<span class=\"%ss%x\">%s</span>", h.prefix, int(tt), html)
} }
}
}, nil }, nil
} }

69
lexers/r.go Normal file

File diff suppressed because one or more lines are too long