Skip to content

Commit

Permalink
Parse highlight matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilliamson committed Jun 13, 2024
1 parent d3a259e commit a26168d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public static StyleMap merge(StyleMap high, StyleMap low) {
Lists.eagerConcat(high.paragraphStyles, low.paragraphStyles),
Lists.eagerConcat(high.runStyles, low.runStyles),
Lists.eagerConcat(high.tableStyles, low.tableStyles),
Lists.eagerConcat(high.breakStyles, low.breakStyles)
Lists.eagerConcat(high.breakStyles, low.breakStyles),
Lists.eagerConcat(high.highlightStyles, low.highlightStyles)
);
}

Expand All @@ -47,6 +48,7 @@ public static StyleMap merge(StyleMap high, StyleMap low) {
private final List<StyleMapping<Run>> runStyles;
private final List<StyleMapping<Table>> tableStyles;
private final List<StyleMapping<Break>> breakStyles;
private final List<StyleMapping<String>> highlightStyles;

public StyleMap(
Optional<HtmlPath> bold,
Expand All @@ -59,7 +61,8 @@ public StyleMap(
List<StyleMapping<Paragraph>> paragraphStyles,
List<StyleMapping<Run>> runStyles,
List<StyleMapping<Table>> tableStyles,
List<StyleMapping<Break>> breakStyles
List<StyleMapping<Break>> breakStyles,
List<StyleMapping<String>> highlightStyles
)
{
this.bold = bold;
Expand All @@ -73,6 +76,7 @@ public StyleMap(
this.runStyles = runStyles;
this.tableStyles = tableStyles;
this.breakStyles = breakStyles;
this.highlightStyles = highlightStyles;
}

public StyleMap update(StyleMap styleMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class StyleMapBuilder {
private final List<StyleMapping<Run>> runStyles = new ArrayList<>();
private final List<StyleMapping<Table>> tableStyles = new ArrayList<>();
private final List<StyleMapping<Break>> breakStyles = new ArrayList<>();
private final List<StyleMapping<String>> highlightStyles = new ArrayList<>();

public StyleMapBuilder() {
this.bold = Optional.empty();
Expand Down Expand Up @@ -62,6 +63,11 @@ public StyleMapBuilder smallCaps(HtmlPath path) {
return this;
}

public StyleMapBuilder mapHighlight(HighlightMatcher matcher, HtmlPath path) {
this.highlightStyles.add(new StyleMapping<>(matcher, path));
return this;
}

public StyleMapBuilder commentReference(HtmlPath path) {
this.commentReference = Optional.of(path);
return this;
Expand Down Expand Up @@ -99,7 +105,8 @@ public StyleMap build() {
paragraphStyles,
runStyles,
tableStyles,
breakStyles
breakStyles,
highlightStyles
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static BiConsumer<StyleMapBuilder, HtmlPath> parse(TokenIterator<TokenTyp
return StyleMapBuilder::allCaps;
case "small-caps":
return StyleMapBuilder::smallCaps;
case "highlight":
HighlightMatcher highlight = parseHighlightMatcher(tokens);
return (builder, path) -> builder.mapHighlight(highlight, path);
case "comment-reference":
return StyleMapBuilder::commentReference;
case "br":
Expand Down Expand Up @@ -112,6 +115,21 @@ private static boolean parseListType(TokenIterator<TokenType> tokens) {
}
}

private static HighlightMatcher parseHighlightMatcher(TokenIterator<TokenType> tokens) {
Optional<String> color;

if (tokens.trySkip(TokenType.SYMBOL, "[")) {
tokens.skip(TokenType.IDENTIFIER, "color");
tokens.skip(TokenType.SYMBOL, "=");
color = Optional.of(TokenParser.parseString(tokens));
tokens.skip(TokenType.SYMBOL, "]");
} else {
color = Optional.empty();
}

return new HighlightMatcher(color);
}

private static BreakMatcher parseBreakMatcher(TokenIterator<TokenType> tokens) {
tokens.skip(TokenType.SYMBOL, "[");
tokens.skip(TokenType.IDENTIFIER, "type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.zwobble.mammoth.internal.styles.parsing.TokenIterator;
import org.zwobble.mammoth.internal.styles.parsing.TokenType;

import java.util.Optional;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;

Expand Down Expand Up @@ -147,6 +149,30 @@ public void readsSmallCaps() {
);
}

@Test
public void readsHighlightWithoutColor() {
assertThat(
parseDocumentMatcher("highlight"),
deepEquals(
new StyleMapBuilder()
.mapHighlight(new HighlightMatcher(Optional.empty()), HTML_PATH)
.build()
)
);
}

@Test
public void readsHighlightWithColor() {
assertThat(
parseDocumentMatcher("highlight[color='yellow']"),
deepEquals(
new StyleMapBuilder()
.mapHighlight(new HighlightMatcher(Optional.of("yellow")), HTML_PATH)
.build()
)
);
}

private static final HtmlPath HTML_PATH = HtmlPath.element("placeholder");

private StyleMap parseDocumentMatcher(String input) {
Expand Down

0 comments on commit a26168d

Please sign in to comment.