Skip to content

Add support for outputting to Asciidoc format #559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Available on [Docker Hub](https://hub.docker.com/r/openapitools/openapi-diff/) a
```bash
# docker run openapitools/openapi-diff:latest
usage: openapi-diff <old> <new>
--asciidoc <file> export diff as asciidoc in given file
--debug Print debugging information
--error Print error information
--fail-on-changed Fail if API changed but is backward
Expand Down Expand Up @@ -101,6 +102,7 @@ openapi-diff can read OpenAPI specs from JSON files or HTTP URLs.
```bash
$ openapi-diff --help
usage: openapi-diff <old> <new>
--asciidoc <file> export diff as asciidoc in given file
--debug Print debugging information
--error Print error information
-h,--help print this message
Expand Down Expand Up @@ -151,6 +153,8 @@ Add openapi-diff to your POM to show diffs when you test your Maven project. You
<jsonOutputFileName>${project.basedir}/../maven/target/diff.json</jsonOutputFileName>
<!-- Supply file path for markdown output to file if desired. -->
<markdownOutputFileName>${project.basedir}/../maven/target/diff.md</markdownOutputFileName>
<!-- Supply file path for Asciidoc output to file if desired. -->
<asciidocOutputFileName>${project.basedir}/../maven/target/diff.adoc</asciidocOutputFileName>
</configuration>
</execution>
</executions>
Expand Down
18 changes: 14 additions & 4 deletions cli/src/main/java/org/openapitools/openapidiff/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openapitools.openapidiff.core.OpenApiCompare;
import org.openapitools.openapidiff.core.model.ChangedOpenApi;
import org.openapitools.openapidiff.core.output.ConsoleRender;
import org.openapitools.openapidiff.core.output.HtmlRender;
import org.openapitools.openapidiff.core.output.JsonRender;
import org.openapitools.openapidiff.core.output.MarkdownRender;
import org.openapitools.openapidiff.core.output.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -109,6 +106,13 @@ public static void main(String... args) {
.argName("file")
.desc("export diff as json in given file")
.build());
options.addOption(
Option.builder()
.longOpt("asciidoc")
.hasArg()
.argName("file")
.desc("export diff as asciidoc in given file")
.build());

// create the parser
CommandLineParser parser = new DefaultParser();
Expand Down Expand Up @@ -191,6 +195,12 @@ public static void main(String... args) {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
mdRender.render(result, outputStreamWriter);
}
if (line.hasOption("asciidoc")) {
AsciidocRender asciidocRender = new AsciidocRender();
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("asciidoc"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
asciidocRender.render(result, outputStreamWriter);
}
if (line.hasOption("text")) {
FileOutputStream outputStream = new FileOutputStream(line.getOptionValue("text"));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.openapitools.openapidiff.core.output;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AsciidocRender extends MarkdownRenderBase {
private static final Logger LOGGER = LoggerFactory.getLogger(AsciidocRender.class);
private static final String H4 = "==== ";
private static final String H5 = "===== ";
private static final String H6 = "====== ";
private static final String CODE = "`";
private static final String PRE_LI = "*";
private static final String LI = "* ";
private static final String HR = "\n'''\n";

@Override
public String getH4() {
return H4;
}

@Override
public String getH5() {
return H5;
}

@Override
public String getH6() {
return H6;
}

@Override
public String getBlockQuote() {
throw new UnsupportedOperationException(
"Asciidoc does not use blockquote marker;"
+ "instead it uses blocks like this: \n\n----\nQUOTED TEXT\n----\n");
}

@Override
public String getCode() {
return CODE;
}

@Override
public String getPreListItem() {
return PRE_LI;
}

@Override
public String getListItem() {
return LI;
}

@Override
public String getHorizontalRule() {
return HR;
}

@Override
protected String blockquote(String beginning, String text) {
return beginning + "\n----\n" + text.trim() + "\n----\n";
}
}
Loading