|
| 1 | +/* |
| 2 | + * Copyright 2015 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.freshmark; |
| 17 | + |
| 18 | +import java.util.function.Consumer; |
| 19 | +import java.util.regex.Matcher; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +/** A format defined by "tag start" and "tag end" chunks of text. */ |
| 23 | +public class ParserIntronExon extends Parser { |
| 24 | + final String intron, exon; |
| 25 | + final Pattern pattern; |
| 26 | + |
| 27 | + /** |
| 28 | + * A Parser which uses simple intron / exon string to delimit comments. |
| 29 | + * <p> |
| 30 | + * Comment blocks will be parsed using the following regex: |
| 31 | + * <pre> |
| 32 | + * Pattern.quote(intron) + "(.*?)" + Pattern.quote(exon) |
| 33 | + * </pre> |
| 34 | + */ |
| 35 | + public ParserIntronExon(String intron, String exon) { |
| 36 | + this(intron, exon, Pattern.quote(intron) + "(.*?)" + Pattern.quote(exon)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * A Parser with the given comment intron/exon pair, with a custom regex. |
| 41 | + * <p> |
| 42 | + * Usually, you should use the {@link #Parser(String, String)} constructor, |
| 43 | + * unless there are some special rules for how comment blocks are parsed. |
| 44 | + */ |
| 45 | + public ParserIntronExon(String intron, String exon, String regex) { |
| 46 | + this.intron = intron; |
| 47 | + this.exon = exon; |
| 48 | + pattern = Pattern.compile(regex, Pattern.DOTALL); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Given an input string, parses out the body sections from the tag sections. |
| 53 | + * |
| 54 | + * @param rawInput the raw input string |
| 55 | + * @param body called for every chunk of text outside a tag |
| 56 | + * @param tag called for every chunk of text inside a tag |
| 57 | + */ |
| 58 | + @Override |
| 59 | + protected void bodyAndTags(String rawInput, Consumer<String> body, Consumer<String> tag) { |
| 60 | + Matcher matcher = pattern.matcher(rawInput); |
| 61 | + int last = 0; |
| 62 | + while (matcher.find()) { |
| 63 | + if (matcher.start() > last) { |
| 64 | + body.accept(rawInput.substring(last, matcher.start())); |
| 65 | + } |
| 66 | + tag.accept(matcher.group(1)); |
| 67 | + last = matcher.end(); |
| 68 | + } |
| 69 | + if (last < rawInput.length()) { |
| 70 | + body.accept(rawInput.substring(last)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Reassembles a section/script/output chunk back into |
| 76 | + * the full file. |
| 77 | + * |
| 78 | + * @param section |
| 79 | + * @param script |
| 80 | + * @param output |
| 81 | + * @return |
| 82 | + */ |
| 83 | + @Override |
| 84 | + protected String reassemble(String section, String script, String output) { |
| 85 | + // make sure that the compiled output starts and ends with a newline, |
| 86 | + // so that the tags stay separated separated nicely |
| 87 | + if (!output.startsWith("\n")) { |
| 88 | + output = "\n" + output; |
| 89 | + } |
| 90 | + if (!output.endsWith("\n")) { |
| 91 | + output = output + "\n"; |
| 92 | + } |
| 93 | + return intron + " " + section + "\n" + |
| 94 | + script + |
| 95 | + exon + |
| 96 | + output + |
| 97 | + intron + " /" + section + " " + exon; |
| 98 | + } |
| 99 | +} |
0 commit comments