SimpleReferenceParser { public List<AsciiDoctorFileReference> parse(String asciidoctorScript) { List<AsciiDoctorFileReference> list = new ArrayList<AsciiDoctorFileReference>(); if (noIdentifierFound(asciidoctorScript)) { return list; } StringBuilder current = new StringBuilder(); int start=0; int currentPos = -1; for (char c : asciidoctorScript.toCharArray()) { currentPos++; if (inline && c ==']') { int end = currentPos-1; addWhenCurrentNotEmptyAndStartsWithIdentifier(asciidoctorScript, list, current, start, end); current = new StringBuilder(); start =currentPos+1; continue; }else if (c == '\n') { int end = currentPos-1; addWhenCurrentNotEmptyAndStartsWithIdentifier(asciidoctorScript, list, current, start, end); current = new StringBuilder(); start =currentPos+1; continue; } if (current != null) { if (inline) { if (Character.isWhitespace(c) && ! Character.isSpaceChar(c)) { continue; } } current.append(c); if (current.length()==1 && current.charAt(0) != firstChar) { current = null; } if (current!=null && current.length()==identifier.length()){ if (current.indexOf(identifier)!=0){ current=null; } } } } addWhenCurrentNotEmptyAndStartsWithIdentifier(asciidoctorScript, list, current, start, currentPos); return list; } SimpleReferenceParser(String identifier, boolean inline); List<AsciiDoctorFileReference> parse(String asciidoctorScript); static final SimpleReferenceParser INCLUDE_PARSER; static final SimpleReferenceParser PLANTUML_PARSER; static final SimpleReferenceParser DITAA_PARSER; static final SimpleReferenceParser IMAGE_PARSER; }
@Test public void include_target_txt_found_include() throws Exception { String text = "include::label.txt"; List<AsciiDoctorFileReference> result = includeParserToTest.parse(text); assertReferences(result). hasReferences(1). hasReference("include::label.txt"). withPosition(0). withEnd(17); }
@Test public void include_target_txt_new_line_headline1_new_line_include_target2_txt_found_2includes() throws Exception { String text = "include::label.txt\n= headline1\ninclude::target2.txt"; List<AsciiDoctorFileReference> result = includeParserToTest.parse(text); assertReferences(result). hasReferences(2). hasReference("include::label.txt"). withPosition(0). withEnd(17). and(). hasReference("include::target2.txt"). withPosition(31). withEnd(50); }
@Test public void headline1_new_line_include_target_txt_found_include() throws Exception { String text = "headline1\ninclude::label.txt"; List<AsciiDoctorFileReference> result = includeParserToTest.parse(text); assertReferences(result). hasReferences(1). hasReference("include::label.txt"). withPosition(10). withEnd(27); }
@Test public void headline1_new_line_include_target_SPACE_with_SPACE_spaces_txt_found_include() throws Exception { String text = "headline1\ninclude::label with spaces.txt"; List<AsciiDoctorFileReference> result = includeParserToTest.parse(text); assertReferences(result). hasReferences(1). hasReference("include::label with spaces.txt"). withPosition(10). withEnd(39); }
@Test public void headline1_new_line_image_target_SPACE_with_SPACE_spaces_png_found_include() throws Exception { String text = "headline1\nimage::label with spaces.png[]"; List<AsciiDoctorFileReference> result = imageParserToTest.parse(text); assertReferences(result). hasReferences(1). hasReference("image::label with spaces.png"). withPosition(10). withEnd(38); }