org.asciidoctor.ast.Cursor Java Examples
The following examples show how to use
org.asciidoctor.ast.Cursor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 6 votes |
/** * @param threadContext * @param args */ @JRubyMethod(name = "add", required = 1, optional = 2) public IRubyObject add(final ThreadContext threadContext, final IRubyObject[] args, Block block) { final IRubyObject rubyMessage; if (args.length >= 2 && !args[1].isNil()) { rubyMessage = args[1]; } else if (block.isGiven()) { rubyMessage = block.yield(threadContext, getRuntime().getNil()); } else { rubyMessage = args[2]; } final Cursor cursor = getSourceLocation(rubyMessage); final String message = formatMessage(rubyMessage); final Severity severity = mapRubyLogLevel(args[0]); final LogRecord record = createLogRecord(threadContext, severity, cursor, message); rootLogHandler.log(record); return getRuntime().getNil(); }
Example #2
Source File: LogRecordHelper.java From asciidoctor-maven-plugin with Apache License 2.0 | 6 votes |
/** * Formats the logRecord in a similar manner to original Asciidoctor. * Note: prints the relative path of the file to `sourceDirectory`. * * @param logRecord Asciidoctor logRecord to format * @param sourceDirectory source directory of the converted AsciiDoc document * @return Asciidoctor-like formatted string */ public static String format(LogRecord logRecord, File sourceDirectory) { final Cursor cursor = logRecord.getCursor(); final String relativePath = calculateFileRelativePath(cursor, sourceDirectory); final List<String> messageParts = new ArrayList<>(); messageParts.add(MESSAGE_HEADER); messageParts.add(logRecord.getSeverity().toString()); if (relativePath != null) messageParts.add(relativePath); if (cursor != null && cursor.getLineNumber() > 0) messageParts.add("line " + cursor.getLineNumber()); messageParts.add(logRecord.getMessage()); return messageParts.stream().collect(Collectors.joining(": ")); }
Example #3
Source File: WhenAsciidoctorLogsToConsole.java From asciidoctorj with Apache License 2.0 | 6 votes |
@Test public void a_extension_should_be_able_to_log() throws Exception { final List<LogRecord> logRecords = new ArrayList<>(); asciidoctor.registerLogHandler(logRecords::add); asciidoctor.javaExtensionRegistry().block(LoggingProcessor.class); String renderContent = asciidoctor.convert("= Test\n\n== Something different\n\n[big]\nHello World", options().option("sourcemap", "true").asMap()); assertEquals(1, logRecords.size()); assertThat(logRecords.get(0).getMessage(), is("Hello Log")); final Cursor cursor = logRecords.get(0).getCursor(); assertThat(cursor.getLineNumber(), is(3)); assertThat(renderContent, containsString("HELLO WORLD")); }
Example #4
Source File: WhenAsciidoctorLogsToConsole.java From asciidoctorj with Apache License 2.0 | 6 votes |
@Test public void shouldLogInvalidRefs() throws Exception { final List<LogRecord> logRecords = new ArrayList<>(); asciidoctor.registerLogHandler(logRecords::add); File inputFile = classpath.getResource("documentwithinvalidrefs.adoc"); String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) .safe(SafeMode.SERVER) .toFile(false) .attributes( AttributesBuilder.attributes().allowUriRead(true)) .asMap()); assertThat(logRecords, hasSize(1)); assertThat(logRecords.get(0).getMessage(), containsString("invalid reference: invalidref")); final Cursor cursor = logRecords.get(0).getCursor(); assertThat(cursor, is(nullValue())); }
Example #5
Source File: WhenAsciidoctorLogsToConsole.java From asciidoctorj with Apache License 2.0 | 5 votes |
@Test public void shouldNotifyLogHandler() throws Exception { final List<LogRecord> logRecords = new ArrayList<>(); asciidoctor.registerLogHandler(logRecords::add); File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) .safe(SafeMode.SERVER) .attributes( AttributesBuilder.attributes().allowUriRead(true)) .asMap()); File expectedFile = new File(inputFile.getParent(), "documentwithnotexistingfile.html"); expectedFile.delete(); assertEquals(4, logRecords.size()); assertThat(logRecords.get(0).getMessage(), containsString("include file not found")); final Cursor cursor = logRecords.get(0).getCursor(); assertThat(cursor.getDir().replace('\\', '/'), is(inputFile.getParent().replace('\\', '/'))); assertThat(cursor.getFile(), is(inputFile.getName())); assertThat(cursor.getLineNumber(), is(3)); for (LogRecord logRecord : logRecords) { assertThat(logRecord.getCursor(), not(nullValue())); assertThat(logRecord.getCursor().getFile(), not(nullValue())); assertThat(logRecord.getCursor().getDir(), not(nullValue())); } }
Example #6
Source File: LogRecordHelperTest.java From asciidoctor-maven-plugin with Apache License 2.0 | 5 votes |
@Test public void should_format_logRecords_with_empty_lineNumber_absolute_path_when_sourceDir_is_not_valid2() throws IOException { // given final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 0, "path", "dir"); final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message"); final File sourceDir = Mockito.mock(File.class); Mockito.when(sourceDir.getCanonicalPath()).thenThrow(new IOException()); // when String formattedLogRecord = LogRecordHelper.format(logRecord, sourceDir); // then assertThat(normalizePath(formattedLogRecord)).matches("asciidoctor: INFO: .*/asciidoctor-maven-plugin/file.adoc: a message"); }
Example #7
Source File: LogRecordHelperTest.java From asciidoctor-maven-plugin with Apache License 2.0 | 5 votes |
@Test public void should_format_full_logRecord_with_file_absolute_path_when_sourceDir_is_not_valid() throws IOException { // given final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 3, "path", "dir"); final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message"); final File sourceDir = Mockito.mock(File.class); Mockito.when(sourceDir.getCanonicalPath()).thenThrow(new IOException()); // when String formattedLogRecord = LogRecordHelper.format(logRecord, sourceDir); // then assertThat(normalizePath(formattedLogRecord)).matches("asciidoctor: INFO: .*/asciidoctor-maven-plugin/file.adoc: line 3: a message"); }
Example #8
Source File: LogRecordHelperTest.java From asciidoctor-maven-plugin with Apache License 2.0 | 5 votes |
@Test public void should_apply_simple_format_when_cursor_is_empty() { // given final Cursor cursor = new TestCursor(null, 0, null, null); final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message"); // when String formattedLogRecord = LogRecordHelper.format(logRecord, null); // then assertThat(normalizePath(formattedLogRecord)).isEqualTo("asciidoctor: INFO: a message"); }
Example #9
Source File: LogRecordHelperTest.java From asciidoctor-maven-plugin with Apache License 2.0 | 5 votes |
@Test public void should_apply_full_format_logRecord_with_all_data() { // given final Cursor cursor = new TestCursor(new File("file.adoc").getAbsolutePath(), 3, "path", "dir"); final LogRecord logRecord = new LogRecord(Severity.INFO, cursor, "a message"); final File sourceDir = getParentFile(); // when String formattedLogRecord = LogRecordHelper.format(logRecord, sourceDir); // then assertThat(normalizePath(formattedLogRecord)).isEqualTo("asciidoctor: INFO: asciidoctor-maven-plugin/file.adoc: line 3: a message"); }
Example #10
Source File: LogRecordHelper.java From asciidoctor-maven-plugin with Apache License 2.0 | 5 votes |
private static String calculateFileRelativePath(Cursor cursor, File sourceDirectory) { try { if (cursor != null && cursor.getFile() != null) { return new File(cursor.getFile()) .getCanonicalPath() .substring(sourceDirectory.getCanonicalPath().length() + 1); } } catch (IOException e) { // use the absolute path as fail-safe return cursor.getFile(); } return null; }
Example #11
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 5 votes |
private Cursor getSourceLocation(IRubyObject msg) { if (getRuntime().getHash().equals(msg.getType())) { final RubyHash hash = (RubyHash) msg; final Object sourceLocation = hash.get(getRuntime().newSymbol(LOG_PROPERTY_SOURCE_LOCATION)); return new CursorImpl((IRubyObject) sourceLocation); } return null; }
Example #12
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 5 votes |
private LogRecord createLogRecord(final ThreadContext threadContext, final Severity severity, final Cursor cursor, final String message) { final Optional<BacktraceElement> elem = threadContext.getBacktrace(0) .skip(1) .findFirst(); final String sourceFileName = elem.map(BacktraceElement::getFilename).orElse(null); final String sourceMethodName = elem.map(BacktraceElement::getMethod).orElse(null); final LogRecord record = new LogRecord(severity, cursor, message, sourceFileName, sourceMethodName); return record; }
Example #13
Source File: JavaLogger.java From asciidoctorj with Apache License 2.0 | 5 votes |
private IRubyObject log(ThreadContext threadContext, IRubyObject[] args, Block block, Severity severity) { final IRubyObject rubyMessage; if (block.isGiven()) { rubyMessage = block.yield(threadContext, getRuntime().getNil()); } else { rubyMessage = args[0]; } final Cursor cursor = getSourceLocation(rubyMessage); final String message = formatMessage(rubyMessage); final LogRecord record = createLogRecord(threadContext, severity, cursor, message); rootLogHandler.log(record); return getRuntime().getNil(); }
Example #14
Source File: StructuralNodeImpl.java From asciidoctorj with Apache License 2.0 | 5 votes |
@Override public Cursor getSourceLocation() { IRubyObject object = getRubyProperty("source_location"); if (object == null || object.isNil()) { return null; } return new CursorImpl(object); }
Example #15
Source File: LogRecord.java From asciidoctorj with Apache License 2.0 | 5 votes |
public LogRecord(Severity severity, Cursor cursor, String message, String sourceFileName, String sourceMethodName) { this.severity = severity; this.cursor = cursor; this.message = message; this.sourceFileName = sourceFileName; this.sourceMethodName = sourceMethodName; }
Example #16
Source File: StructuralNodeImpl.java From swagger2markup with Apache License 2.0 | 4 votes |
@Override public Cursor getSourceLocation() { return new CursorImpl(); }
Example #17
Source File: WhenAsciidoctorLogsToConsole.java From asciidoctorj with Apache License 2.0 | 4 votes |
@Test public void shouldOnlyNotifyFromRegisteredAsciidoctor() throws Exception { final List<LogRecord> logRecords = new ArrayList<>(); final Asciidoctor secondInstance = Asciidoctor.Factory.create(); asciidoctor.registerLogHandler(logRecords::add); // Now render via second instance and check that there is no notification File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); String renderContent1 = secondInstance.convertFile(inputFile, options() .inPlace(true) .safe(SafeMode.SERVER) .attributes( AttributesBuilder.attributes().allowUriRead(true)) .asMap()); File expectedFile1 = new File(inputFile.getParent(), "documentwithnotexistingfile.html"); expectedFile1.delete(); assertEquals(0, logRecords.size()); // Now render via first instance and check that notifications appeared. String renderContent = asciidoctor.convertFile(inputFile, options() .inPlace(true) .safe(SafeMode.SERVER) .attributes( AttributesBuilder.attributes().allowUriRead(true)) .asMap()); File expectedFile2 = new File(inputFile.getParent(), "documentwithnotexistingfile.html"); expectedFile2.delete(); assertEquals(4, logRecords.size()); assertThat(logRecords.get(0).getMessage(), containsString("include file not found")); final Cursor cursor = (Cursor) logRecords.get(0).getCursor(); assertThat(cursor.getDir().replace('\\', '/'), is(inputFile.getParent().replace('\\', '/'))); assertThat(cursor.getFile(), is(inputFile.getName())); assertThat(cursor.getLineNumber(), is(3)); }
Example #18
Source File: LogRecord.java From asciidoctorj with Apache License 2.0 | 4 votes |
/** * @return Information about the location of the event */ public Cursor getCursor() { return cursor; }
Example #19
Source File: LogRecord.java From asciidoctorj with Apache License 2.0 | 4 votes |
public LogRecord(Severity severity, Cursor cursor, String message) { this.severity = severity; this.cursor = cursor; this.message = message; }