SimpleInlineAnchorParser { public List<AsciiDoctorInlineAnchor> parse(String asciidoctorScript) { List<AsciiDoctorInlineAnchor> list = new ArrayList<AsciiDoctorInlineAnchor>(); if (asciidoctorScript == null || asciidoctorScript .length() < 5) { return list; } StringBuilder current = new StringBuilder(); int start = 0; int currentPos = -1; for (char c : asciidoctorScript.toCharArray()) { currentPos++; if (c == '\n') { int end = currentPos - 1; addWhenCurrentWhenValidAnchorText(asciidoctorScript, list, current, start, end); current = new StringBuilder(); start = currentPos + 1; continue; } if (current != null) { current.append(c); if (current.length() == 1 && current.charAt(0) != '[') { current = null; } else if (current.length() == 2 && (current.charAt(1) != '[' && current.charAt(1) != '#')) { current = null; } } } addWhenCurrentWhenValidAnchorText(asciidoctorScript, list, current, start, currentPos); return list; }  List<AsciiDoctorInlineAnchor> parse(String asciidoctorScript);  }
@Test public void inline_anchor_section_13() throws Exception { String text = "[[section-13]]"; List<AsciiDoctorInlineAnchor> result = parserToTest.parse(text); assertInlineAnchors(result). hasInlineAnchors(1). hasInlineAnchor("[[section-13]]"). withId("section-13"). withPosition(0). withEnd(13); }
@Test public void parser_handles_2_inline_anchors() throws Exception { String text = "[[section-alpha-bravo]]\n==Headline\n\n[[section-delta-gamma]]\ntext\n[[wrong]"; List<AsciiDoctorInlineAnchor> result = parserToTest.parse(text); assertInlineAnchors(result). hasInlineAnchors(2). hasInlineAnchor("[[section-alpha-bravo]]"). withId("section-alpha-bravo"). withPosition(0). withEnd(22). and(). hasInlineAnchor("[[section-delta-gamma]]"). withPosition(36). withEnd(58); }
@Test public void custom_anchor_section_13() throws Exception { String text = "[#section-13]"; List<AsciiDoctorInlineAnchor> result = parserToTest.parse(text); assertInlineAnchors(result). hasInlineAnchors(1). hasInlineAnchor("[#section-13]"). withId("section-13"). withPosition(0). withEnd(12); }
@Test public void parser_handles_2_custom_anchors() throws Exception { String text = "[#section-alpha-bravo]\n==Headline\n\n[#section-delta-gamma]\ntext\n[[wrong]"; List<AsciiDoctorInlineAnchor> result = parserToTest.parse(text); assertInlineAnchors(result). hasInlineAnchors(2). hasInlineAnchor("[#section-alpha-bravo]"). withId("section-alpha-bravo"). withPosition(0). withEnd(21). and(). hasInlineAnchor("[#section-delta-gamma]"). withPosition(35). withEnd(56); }