1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-02-05 13:05:18 +02:00

101 Commits

Author SHA1 Message Date
guangwu
b8ad4d6cab
chore: unnecessary use of fmt.Sprintf (#799) 2023-07-10 18:42:13 +10:00
guangwu
b9a63eb4f0
chore: remove refs to deprecated io/ioutil (#798) 2023-07-10 18:42:01 +10:00
Itamar Haber
633f8e0f55 Removes 'tabindex="0"' from `<pre>' elements 2023-01-20 02:03:49 +11:00
Siavash Askari Nasr
0e2db44744 Add -webkit-user-select: none; to support Safari 2023-01-11 02:00:54 +11:00
Alec Thomas
c263f6fa19
feat: XML style definitions (#693)
Fixes #635.
2022-11-01 21:23:14 -07:00
Alec Thomas
d0e811c0ef
fix: use a class with line links when requested (#692)
Fixes #683
2022-10-31 23:28:30 -07:00
Alec Thomas
40b7a9135a Upgrade project to Go 1.19 and switch to alecthomas/assert/v2 2022-10-18 08:55:38 +11:00
Siavash Askari Nasr
d18e8a46f2 Add InlineCode option for inline code blocks
- Add an `InlineCode` option for inline code blocks
- When `PreventSurroundingPre` option is enabled, do not wrap the code by `Line` and `CodeLine`
- Write and update related tests
2022-06-14 21:17:50 +10:00
Siavash Askari Nasr
530c45d213 Don't append semicolon to styleEntry if there is no other style
This means we also need to append semicolon to `Background`.
2022-06-14 15:34:02 +10:00
Siavash Askari Nasr
5397b4880b Append semicolon to tabWidthStyle. 2022-06-14 15:34:02 +10:00
Siavash Askari Nasr
c78b8a6a03 Fix extra semicolon in tabWidthStyle
Needed after changes in #636
2022-06-14 07:16:12 +10:00
Siavash Askari Nasr
dea6a13339 Add CSS style inheritance to WithCustomCSS option 2022-05-18 02:45:33 -07:00
Siavash Askari Nasr
7d779408db [html formatter] Add option to let users provide custom CSS styles
Example: WithCustomCSS(map[chroma.TokenType]string{chroma.Line: `display: inline;`})
2022-05-18 02:45:33 -07:00
Alec Thomas
cc2dd5b8ad Version 2 of Chroma
This cleans up the API in general, removing a bunch of deprecated stuff,
cleaning up circular imports, etc.

But the biggest change is switching to an optional XML format for the
regex lexer.

Having lexers defined only in Go is not ideal for a couple of reasons.
Firstly, it impedes a significant portion of contributors who use Chroma
in Hugo, but don't know Go. Secondly, it bloats the binary size of any
project that imports Chroma.

Why XML? YAML is an abomination and JSON is not human editable. XML
also compresses very well (eg. Go template lexer XML compresses from
3239 bytes to 718).

Why a new syntax format? All major existing formats rely on the
Oniguruma regex engine, which is extremely complex and for which there
is no Go port.

Why not earlier? Prior to the existence of fs.FS this was not a viable
option.

Benchmarks:

    $ hyperfine --warmup 3 \
        './chroma.master --version' \
        './chroma.xml-pre-opt --version' \
        './chroma.xml --version'
    Benchmark 1: ./chroma.master --version
      Time (mean ± σ):       5.3 ms ±   0.5 ms    [User: 3.6 ms, System: 1.4 ms]
      Range (min … max):     4.2 ms …   6.6 ms    233 runs

    Benchmark 2: ./chroma.xml-pre-opt --version
      Time (mean ± σ):      50.6 ms ±   0.5 ms    [User: 52.4 ms, System: 3.6 ms]
      Range (min … max):    49.2 ms …  51.5 ms    51 runs

    Benchmark 3: ./chroma.xml --version
      Time (mean ± σ):       6.9 ms ±   1.1 ms    [User: 5.1 ms, System: 1.5 ms]
      Range (min … max):     5.7 ms …  19.9 ms    196 runs

    Summary
      './chroma.master --version' ran
        1.30 ± 0.23 times faster than './chroma.xml --version'
        9.56 ± 0.83 times faster than './chroma.xml-pre-opt --version'

A slight increase in init time, but I think this is okay given the
increase in flexibility.

And binary size difference:

    $ du -h lexers.test*
    $ du -sh chroma*                                                                                                                                                                                                                                                                                                                                                                                                                                                             951371ms
    8.8M	chroma.master
    7.8M	chroma.xml
    7.8M	chroma.xml-pre-opt

Benchmarks:

    $ hyperfine --warmup 3 \
        './chroma.master --version' \
        './chroma.xml-pre-opt --version' \
        './chroma.xml --version'
    Benchmark 1: ./chroma.master --version
      Time (mean ± σ):       5.3 ms ±   0.5 ms    [User: 3.6 ms, System: 1.4 ms]
      Range (min … max):     4.2 ms …   6.6 ms    233 runs

    Benchmark 2: ./chroma.xml-pre-opt --version
      Time (mean ± σ):      50.6 ms ±   0.5 ms    [User: 52.4 ms, System: 3.6 ms]
      Range (min … max):    49.2 ms …  51.5 ms    51 runs

    Benchmark 3: ./chroma.xml --version
      Time (mean ± σ):       6.9 ms ±   1.1 ms    [User: 5.1 ms, System: 1.5 ms]
      Range (min … max):     5.7 ms …  19.9 ms    196 runs

    Summary
      './chroma.master --version' ran
        1.30 ± 0.23 times faster than './chroma.xml --version'
        9.56 ± 0.83 times faster than './chroma.xml-pre-opt --version'

Incompatible changes:

- (*RegexLexer).SetAnalyser: changed from func(func(text string) float32) *RegexLexer to func(func(text string) float32) Lexer
- (*TokenType).UnmarshalJSON: removed
- Lexer.AnalyseText: added
- Lexer.SetAnalyser: added
- Lexer.SetRegistry: added
- MustNewLazyLexer: removed
- MustNewLexer: changed from func(*Config, Rules) *RegexLexer to func(*Config, func() Rules) *RegexLexer
- Mutators: changed from func(...Mutator) MutatorFunc to func(...Mutator) Mutator
- NewLazyLexer: removed
- NewLexer: changed from func(*Config, Rules) (*RegexLexer, error) to func(*Config, func() Rules) (*RegexLexer, error)
- Pop: changed from func(int) MutatorFunc to func(int) Mutator
- Push: changed from func(...string) MutatorFunc to func(...string) Mutator
- TokenType.MarshalJSON: removed
- Using: changed from func(Lexer) Emitter to func(string) Emitter
- UsingByGroup: changed from func(func(string) Lexer, int, int, ...Emitter) Emitter to func(int, int, ...Emitter) Emitter
2022-01-27 15:22:00 +11:00
Siavash Askari Nasr
d6e61d3cbe Use span for .line, put code inside <code>
Apparently only "phrasing content" are permitted inside `pre` and
`code`, and `div` is not one of them, so replace it with `span`, `div`
wasn't necessary anyway.

Also it makes sense to put code inside `code` tag(except for line
numbers).

Removed `overflow:auto` and `width: auto` as they didn't seem to be
necessary and actually prevented horizontal scroll bar to appear when content
didn't fit in the viewport.
2021-11-10 19:52:49 +11:00
Siavash Askari Nasr
5ed5054d73 Put lines in a div & code lines in a span, Add WrapLongLines Option 2021-11-09 19:14:33 +11:00
Alec Thomas
54fd3bc12f Clean up imports and make cmd/chroma its own module. 2021-06-18 16:56:50 +10:00
rhymes
c8581d47ee Add tabindex="0" to default <pre> wrapper 2021-05-24 07:16:54 +10:00
Alec Thomas
7e282be495 Update golangci-lint so we can force use of LazyLexer. 2021-04-29 12:08:28 +10:00
Alec Thomas
5da831672d Fix a few bugs including sub-lexers adding additional newlines when
EnsureNL is true.
2021-02-06 20:13:50 +11:00
Alvaro Aleman
ab61726cdb Make linkeable lines a link to themselves
Currently its already possible to make line lumbers linkeable. Getting
such a link however requires the end user to look at the pages source
and then to manually edit the URL, which is not a great UX.

This PR changes that to make the line numbers a link to themselves, so
clicking on them gives a link to that line that can then be passed
around, similiar to e.G. GitHub.
2020-11-03 21:31:04 +11:00
Cameron Little
809ff9ba45 Don't emit styles that are missing a class 2020-05-09 22:56:49 +10:00
Cameron Little
cc5e532d14 Add failing test for invalid css with allClasses
This was reported at https://github.com/gohugoio/hugo/issues/7207
2020-05-09 22:56:49 +10:00
Alec Thomas
cc53faa932 Add option to preserve all HTML classes.
Fixes #346.
2020-04-13 07:36:37 +10:00
Pablo Santiago Blum de Aguiar
9e22bd6e2f Consider baseLineNumber when calculating the column width 2020-01-10 08:23:54 +11:00
Alexandru Băluț
19d5ee688c Highlight the line number targeted by the URL anchor
This currently works only when `html.WithClasses(true)` is used.
The same colors as for the pre-highlighted lines are used.
Only the line number is highlighted, so difficult to notice, but
better than nothing.
2019-12-31 11:10:38 +11:00
Alexandru Băluț
a41c89767d Add the option of making the line numbers linkable 2019-12-12 17:31:43 -08:00
Bjørn Erik Pedersen
5921c52787 Add bool argument to WithClasses, WithLineNumbers etc.
This allows the boolean options to be reconfigured, e.g:

```go
options := getOptions()
options = append(options, html.WithLineNumbers(true))
```

Fixes #301
2019-11-22 10:13:00 +11:00
Bjørn Erik Pedersen
d3926cc0e1 Add WithPreWrapper option 2019-11-20 20:09:17 +11:00
Alec Thomas
bbc59ac372 Emit error tokens when there's a group mismatch.
Also don't panic/recover, as we no longer use panic to report "real"
errors.

Fixes #295.
2019-10-24 17:03:35 +11:00
rsteube
aaa94ce97c svg: fixed text background rectangles
- location and size looks fine now even when scaled and with larger files (used `em` for line height and `ch` for
width)
- svg width/height looks ok now, width has some problems on scaling (seems this needs to be a unit like `px` and cannot be `ch` so it's a rough estimate)
- updated liberation mono font to 2.00.5 since the current one had wrong width for the full block character (https://github.com/liberationfonts/liberation-fonts)
2019-10-16 21:09:59 +11:00
rsteube
611a24ccdd embedded font in svg (#292) 2019-10-15 23:38:42 +11:00
Alec Thomas
73d11b3c45 Clear background colour for TTY formatters. 2019-10-15 21:08:17 +11:00
Alec Thomas
b44683eae8 Fix linter. 2019-10-06 20:14:05 +11:00
rsteube
22511fb8e4 added svg formatter
basic version without any options. colors and font-styles seem to be ok.
rough support for text background in styles like `murphy` using predrawn
rectangles (svg has no text background attribute).

things to improve:
- svg width attribute (`<svg width=""`)
- linenumbers
- highlighting
- embedded font
- tabwidth option
- margins?
- better position/width calculation (rectangles not correctly drawn on
resize)
2019-09-30 09:43:27 +10:00
Ray Lee
2f5349aa18 Add italics support for indexed colour terminals 2019-09-02 08:12:43 +10:00
Ray Lee
f2e05136f7 Add italics support for truecolour terminals 2019-09-02 08:12:43 +10:00
Alec Thomas
e6a56651ad Add width:100% to content <td> when inlining styles.
Fixes #225.
2019-07-20 23:19:20 +10:00
Alec Thomas
da5ac60d8c Add golangci-lint and fix all lint issues. 2018-12-31 22:46:59 +11:00
Alec Thomas
2a59c2c77e Synthesise meta style-entries on demand.
This includes line highlighting, numbers, etc.

Fixes #211.
2018-12-31 11:33:36 +11:00
Alec Thomas
69c9a262c3 Expose Colour.BrightenOrDarken (useful for #211). 2018-12-31 10:48:45 +11:00
Alec Thomas
881a441774 Don't keep trailing empty tokens when splitting tokens by line.
Fixes #155. Fixes #209.
2018-12-27 16:20:38 +11:00
Alec Thomas
fb4c38e204 Fix build (see #190). 2018-11-08 17:16:58 +11:00
Randall C. O'Reilly
a4f179974b move SplitTokensIntoLines into chroma/iterator.go, fixes issue #190 2018-11-08 16:21:45 +11:00
Daniel Eloff
9c3abeae1d Tokens by value (#187)
This results in about a 8% improvement in speed.
2018-11-04 10:22:51 +11:00
Felix Knischewski
e36baa268a Add option to prevent surroundign pre tags 2018-09-18 19:44:44 +10:00
Kaushal Modi
371820dad6 Assign .gl class to GenericUnderline; add CSS rules for the same
'l' in gl is for under(l)ine, as the "gu" class is taken by
GenericSubheading.

- Rules for GenericUnderline are added to all the styles
- Make "Underline" style insert "text-decoration: underline" in CSS.

Fixes https://github.com/alecthomas/chroma/issues/159.
2018-08-01 17:28:52 -04:00
Alec Thomas
0961f82bc3 Fix nearest colour matching.
This was caused by precision loss due to using uint8. The cast to int64
was occurring in the wrong location.

Fixes #145.
2018-06-25 20:48:31 +10:00
Alec Thomas
3020e2ea8c Fix bug with nested newlines.
Fixes #124.

Also reinstitute lexer tests that disappeared during package split.
2018-03-03 10:16:21 +11:00
John Millikin
aaa96c6984 Avoid adding an HTML prefix to empty class names.
This makes the raw markup a bit cleaner when there's lots of `Text`
tokens.
2018-02-25 19:46:48 +11:00