Skip to content

Commit

Permalink
Support style mapping for highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Jun 13, 2024
1 parent 43f5e18 commit 5edcc79
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 1.8.0

* Support parsing of run highlights.
* Add style mapping for highlights.

# 1.7.1

Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,43 @@ small-caps
Note that this matches text that has had small caps explicitly applied to it.
It will not match any text that is small caps because of its paragraph or run style.

#### Highlight

Match explicitly highlighted text:

```
highlight
```

Note that this matches text that has had a highlight explicitly applied to it.
It will not match any text that is highlighted because of its paragraph or run style.

It's also possible to match specific colours.
For instance, to match yellow highlights:

```
highlight[color='yellow']
```

The set of colours typically used are:

* `black`
* `blue`
* `cyan`
* `green`
* `magenta`
* `red`
* `yellow`
* `white`
* `darkBlue`
* `darkCyan`
* `darkGreen`
* `darkMagenta`
* `darkRed`
* `darkYellow`
* `darkGray`
* `lightGray`

#### Ignoring document elements

Use `!` to ignore a document element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ public List<HtmlNode> visit(Paragraph paragraph, Context context) {
@Override
public List<HtmlNode> visit(Run run, Context context) {
Supplier<List<HtmlNode>> nodes = () -> convertChildrenToHtml(run, context);
if (run.getHighlight().isPresent()) {
nodes = styleMap.getHighlightHtmlPath(run.getHighlight().get())
.orElse(HtmlPath.EMPTY)
.wrap(nodes);
}
if (run.isSmallCaps()) {
nodes = styleMap.getSmallCaps().orElse(HtmlPath.EMPTY).wrap(nodes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,9 @@ public Optional<HtmlPath> getBreakHtmlPath(Break breakElement) {
return tryFind(breakStyles, styleMapping -> styleMapping.matches(breakElement))
.map(StyleMapping::getHtmlPath);
}

public Optional<HtmlPath> getHighlightHtmlPath(String color) {
return tryFind(highlightStyles, styleMapping -> styleMapping.matches(color))
.map(StyleMapping::getHtmlPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,50 @@ public void smallCapsCanBeMappedUsingStyleMapping() {
deepEquals(list(Html.element("span", list(Html.text("Hello"))))));
}

@Test
public void highlightedRunsAreIgnoredByDefault() {
Run run = run(withHighlight("yellow"), withChildren(new Text("Hello")));

List<HtmlNode> result = convertToHtml(run);

assertThat(result, deepEquals(list(Html.text("Hello"))));
}

@Test
public void highlightedRunsCanBeConfiguredWithStyleMappingForAllHighlights() {
Run run = run(withHighlight("yellow"), withChildren(new Text("Hello")));
StyleMap styleMap = StyleMap.builder()
.mapHighlight(new HighlightMatcher(Optional.empty()), HtmlPath.element("mark"))
.build();

List<HtmlNode> result = convertToHtml(run, styleMap);

assertThat(result, deepEquals(list(
Html.element("mark", list(Html.text("Hello")))
)));
}

@Test
public void highlightedRunsCanBeConfiguredWithStyleMappingForSpecificHighlightColor() {
Paragraph paragraph = paragraph(withChildren(
run(withHighlight("yellow"), withChildren(new Text("Yellow"))),
run(withHighlight("red"), withChildren(new Text("Red")))
));
StyleMap styleMap = StyleMap.builder()
.mapHighlight(new HighlightMatcher(Optional.of("yellow")), HtmlPath.element("mark", map("class", "yellow")))
.mapHighlight(new HighlightMatcher(Optional.empty()), HtmlPath.element("mark"))
.build();

List<HtmlNode> result = convertToHtml(paragraph, styleMap);

assertThat(result, deepEquals(list(
Html.element("p", list(
Html.element("mark", map("class", "yellow"), list(Html.text("Yellow"))),
Html.element("mark", list(Html.text("Red")))
))
)));
}

@Test
public void superscriptRunsAreWrappedInSuperscriptTags() {
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ public static Argument<Boolean> withSmallCaps(boolean smallCaps) {
return arg(SMALL_CAPS, smallCaps);
}

public static Argument<Optional<String>> withHighlight(String highlight) {
return arg(HIGHLIGHT, Optional.of(highlight));
}

public static Argument<VerticalAlignment> withVerticalAlignment(VerticalAlignment verticalAlignment) {
return arg(VERTICAL_ALIGNMENT, verticalAlignment);
}
Expand Down

0 comments on commit 5edcc79

Please sign in to comment.