Skip to content

Commit

Permalink
add diff syntax highlighter
Browse files Browse the repository at this point in the history
  • Loading branch information
jenshalm committed Mar 25, 2024
1 parent faad62d commit 3a455dd
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ case object SyntaxHighlighting extends ExtensionBundle { self =>
XMLSyntax,
YAMLSyntax,
ShellSyntax,
DiffSyntax,
DhallSyntax,
SQLSyntax,
EBNFSyntax,
Expand Down
12 changes: 12 additions & 0 deletions core/shared/src/main/scala/laika/parse/code/CodeCategory.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ object CodeCategory {

}

/** Categories specific to diff highlighting. */
object Diff {

sealed trait DiffCategory extends CodeCategory {
override def prefix: String = "diff-"
}

case object Added extends DiffCategory
case object Removed extends DiffCategory

}

/** Categories for text markup formats. */
object Markup {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package laika.parse.code.languages

import cats.data.NonEmptyList
import laika.api.bundle.SyntaxHighlighter
import laika.parse.builders.*
import laika.parse.code.{ CodeCategory, CodeSpanParser }

/** Simple highlighter for diff syntax where added and removed lines
* are indicated with a '+' or '-' line prefix.
*
* @author Jens Halm
*/
object DiffSyntax extends SyntaxHighlighter {

val language: NonEmptyList[String] = NonEmptyList.of("diff")

private def prefixedLine(startChar: Char, category: CodeCategory): CodeSpanParser =
CodeSpanParser.onLineStart(category) {
(oneOf(startChar) ~ anyNot('\n')).source
}

val spanParsers: Seq[CodeSpanParser] = Seq(
prefixedLine('-', CodeCategory.Diff.Removed),
prefixedLine('+', CodeCategory.Diff.Added)
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,32 @@ class LanguageSpec extends FunSuite {
assertEquals(parse(input), expected)
}

test("Diff") {
def added(value: String): CodeSpan = CodeSpan(value, CodeCategory.Diff.Added)
def removed(value: String): CodeSpan = CodeSpan(value, CodeCategory.Diff.Removed)

val input =
"""# Doc
|
|```diff
|+ 1st added line
|- 1st removed line
|unchanged line
|+ 2nd added line
|```
|""".stripMargin
val expected = result(
"diff",
added("+ 1st added line"),
other("\n"),
removed("- 1st removed line"),
other("\nunchanged line\n"),
added("+ 2nd added line")
)

assertEquals(parse(input), expected)
}

test("Dhall") {
val input =
"""# Doc
Expand Down
2 changes: 1 addition & 1 deletion docs/src/01-about-laika/01-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Content Organization

* Use integrated parsers for [Syntax Highlighting] that work for all output formats.
Supported out of the box are Scala, Dotty, Java, Python, JavaScript (JSX), TypeScript (TSX), Haskell,
HTML, CSS, XML, YAML, JSON, HOCON, SQL, Shell/Bash, EBNF, Alloy, Dhall, Dart
HTML, CSS, XML, YAML, JSON, HOCON, SQL, Shell/Bash, EBNF, Diff, Alloy, Dhall, Dart

* Freely organize and merge content from multiple input directories or generated in-memory
with the library's [Virtual Tree Abstraction].
Expand Down
1 change: 1 addition & 0 deletions docs/src/03-preparing-content/05-syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Laika currently supports the following languages and formats:
* SQL
* Shell/Bash
* EBNF
* Diff
* Alloy
* Dhall
* Dart
Expand Down
2 changes: 1 addition & 1 deletion docs/src/07-reference/03-spec-compliance.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ is integrated into Laika's test suite.
### Supported Standard Directives

Directives are an extension mechanism of reStructuredText and the reference implementation supports a set
of standard [standard directives][std directives] out of the box.
of [standard directives][std directives] out of the box.

[std directives]: http://docutils.sourceforge.net/docs/ref/rst/directives.html

Expand Down
7 changes: 7 additions & 0 deletions io/src/main/resources/laika/helium/css/code.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,10 @@ code .number-literal, .string-literal, .literal-value, .boolean-literal, .char-l
code .type-name, code .tag-name, code .xml-dtd-tag-name, code .markup-fence {
color: var(--syntax-wheel5);
}

code .diff-added {
background-color: rgb(0 175 0 / 40%);
}
code .diff-removed {
background-color: rgb(250 0 0 / 40%);
}

0 comments on commit 3a455dd

Please sign in to comment.