Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To run image:
docker run --rm -w /doc/[workdir] -v $PWD:/doc curs/asciidoctor-idgen idgen [params]
```

To run image as a serve :
To run image as a server:

```
docker run --rm -w /doc/[workdir] -v $PWD:/doc curs/asciidoctor-idgen serveidgen
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>ru.curs.asciidoctor_idgen</groupId>
<artifactId>asciidoctor-idgen</artifactId>
<packaging>jar</packaging>
<version>1.1.3</version>
<version>1.1.4</version>
<name>asciidoctor-idgen</name>

<properties>
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/ru/curs/asciidoctor_idgen/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
@RequestMapping("/api")
public class Controller {

@PostMapping("/enrich")
public ResponseEntity<Object> enrich(
@RequestBody EnrichParams enrichParams
) throws IOException {
if (enrichParams.getInput() == null)
return new ResponseEntity<>("No input provided", HttpStatus.CONFLICT);
if (enrichParams.getOutput() == null)
return new ResponseEntity<>("No output provided", HttpStatus.CONFLICT);
if (enrichParams.getJson() == null)
return new ResponseEntity<>("No json provided", HttpStatus.CONFLICT);
var outputLog = Main.enrich(enrichParams.getInput(), enrichParams.getOutput()
, enrichParams.getJson(), enrichParams.getOhtml() != null);
return new ResponseEntity<>(outputLog, HttpStatus.OK);
}
@PostMapping("/enrich")
public ResponseEntity<Object> enrich(
@RequestBody EnrichParams enrichParams
) throws IOException {
if (enrichParams.getInput() == null)
return new ResponseEntity<>("No input provided", HttpStatus.CONFLICT);
if (enrichParams.getOutput() == null)
return new ResponseEntity<>("No output provided", HttpStatus.CONFLICT);
if (enrichParams.getJson() == null)
return new ResponseEntity<>("No json provided", HttpStatus.CONFLICT);
var outputLog = Main.enrich(enrichParams.getInput(), enrichParams.getOutput()
, enrichParams.getJson(), enrichParams.getOhtml() != null
, enrichParams.getSectionNaturalIds() != null
);
return new ResponseEntity<>(outputLog, HttpStatus.OK);
}

}
139 changes: 76 additions & 63 deletions src/main/java/ru/curs/asciidoctor_idgen/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,90 +16,103 @@


class Converter {
private String adocFilePath;
private String adocFilePath;

public static String removeFileExtension(String filename, boolean removeAllExtensions) {
if (filename == null || filename.isEmpty()) {
return filename;
}

String extPattern = "(?<!^)[.]" + (removeAllExtensions ? ".*" : "[^.]*$");
return filename.replaceAll(extPattern, "");
public static String removeFileExtension(String filename, boolean removeAllExtensions) {
if (filename == null || filename.isEmpty()) {
return filename;
}

Converter(String adocFilePath) {
this.adocFilePath = adocFilePath;
}
String extPattern = "(?<!^)[.]" + (removeAllExtensions ? ".*" : "[^.]*$");
return filename.replaceAll(extPattern, "");
}

void nodeDescendant(ArrayList<Node> nodes, Node nodeToProcess) {
nodes.add(nodeToProcess);
nodeToProcess.childNodes().forEach(childNode -> nodeDescendant(nodes, childNode));
}
Converter(String adocFilePath) {
this.adocFilePath = adocFilePath;
}

void nodeDescendant(ArrayList<Node> nodes, Node nodeToProcess) {
nodes.add(nodeToProcess);
nodeToProcess.childNodes().forEach(childNode -> nodeDescendant(nodes, childNode));
}

OutputLog convert() {
final OutputLog outputLog = new OutputLog();
try (Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
asciidoctor.registerLogHandler(logRecord -> {
var fullLogMessage =
String.format("%s: %s", logRecord.getSeverity(), logRecord.getMessage());
System.out.println(fullLogMessage);
outputLog.add(logRecord.getSeverity().name(), logRecord.getMessage());
});
OutputLog convert() {
final OutputLog outputLog = new OutputLog();
try (Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
asciidoctor.registerLogHandler(logRecord -> {
var fullLogMessage =
String.format("%s: %s", logRecord.getSeverity(), logRecord.getMessage());
System.out.println(fullLogMessage);
outputLog.add(logRecord.getSeverity().name(), logRecord.getMessage());
});

// val options = Options.builder().backend("html5")
// .safe(SafeMode.UNSAFE).sourcemap(true).toFile(false).standalone(true)
// .attributes(attributes)
// .apply { if (baseDir != null) baseDir(baseDir) }
// .build()

System.out.println(this.adocFilePath);
System.out.println((new File(this.adocFilePath)).getParentFile());
var baseDir = (new File("./" + this.adocFilePath)).getParentFile();
System.out.println(this.adocFilePath);
System.out.println((new File(this.adocFilePath)).getParentFile());
var baseDir = (new File("./" + this.adocFilePath)).getParentFile();

var options = Options.builder()
.safe(SafeMode.UNSAFE)
.baseDir(baseDir)
.build();
var options = Options.builder()
.safe(SafeMode.UNSAFE)
.baseDir(baseDir)
.build();

asciidoctor.convertFile(new File(this.adocFilePath), options);
}
var outputHtmlPath = String.format("%s.html", removeFileExtension(this.adocFilePath, false));
Document jsoup;
try {
jsoup = Jsoup.parse(new File(outputHtmlPath));
} catch (IOException e) {
throw new RuntimeException("Output Html not found", e);
}
System.out.println("Converted to html successfully");
outputLog.add("INFO", "Converted to html successfully");
jsoup.selectXpath("//li").forEach(listItem ->
{
var descendant = new ArrayList<Node>();
nodeDescendant(descendant, listItem);
descendant.stream().filter(node -> node instanceof Element).forEach(child -> {
var childElement = (Element) child;
if (child.nodeName().equals("a") && !childElement.id().isBlank()) {
String id = childElement.id();
childElement.id(String.format("anchor_%s", id));
listItem.id(id);
}
}
);
asciidoctor.convertFile(new File(this.adocFilePath), options);
}
var outputHtmlPath = String.format("%s.html", removeFileExtension(this.adocFilePath, false));
Document jsoup;
try {
jsoup = Jsoup.parse(new File(outputHtmlPath));
} catch (IOException e) {
throw new RuntimeException("Output Html not found", e);
}
System.out.println("Converted to html successfully");
outputLog.add("INFO", "Converted to html successfully");
jsoup.selectXpath("//li").forEach(listItem ->
{
var descendant = new ArrayList<Node>();
nodeDescendant(descendant, listItem);
var anchors = descendant.stream()
.filter(node -> node instanceof Element
&& ((Element) node).tagName().equals("a"))
.toList();
if (!anchors.isEmpty()) {
var child = anchors.get(0);
var childElement = (Element) child;
if (!childElement.id().isBlank()) {
String id = childElement.id();
childElement.id(String.format("anchor_%s", id));
listItem.id(id);
}
}
// descendant.stream().filter(node -> node instanceof Element).forEach(child -> {
// var childElement = (Element) child;
// if (child.nodeName().equals("a") && !childElement.id().isBlank()) {
// String id = childElement.id();
// childElement.id(String.format("anchor_%s", id));
// listItem.id(id);
// }
// }
// );
// var aTagsWithId = listItem.selectXpath(".//a[@id]");
// if (!aTagsWithId.isEmpty()) {
// var aTagWithId = aTagsWithId.get(0);
// String id = aTagWithId.id();
// aTagWithId.id(String.format("anchor_%s", id));
// listItem.id(id);
// }
}

);
try (PrintWriter out = new PrintWriter(outputHtmlPath)) {
out.print(jsoup);
} catch (FileNotFoundException e) {
throw new RuntimeException("Can't overwrite resulting html", e);
}
return outputLog;

);
try (PrintWriter out = new PrintWriter(outputHtmlPath)) {
out.print(jsoup);
} catch (FileNotFoundException e) {
throw new RuntimeException("Can't overwrite resulting html", e);
}
return outputLog;
}
}
5 changes: 5 additions & 0 deletions src/main/java/ru/curs/asciidoctor_idgen/EnrichParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class EnrichParams {
private String output;
private String json;
private Boolean ohtml;
private Boolean sni;

public String getInput() {
return input;
Expand All @@ -21,4 +22,8 @@ public String getJson() {
public Boolean getOhtml() {
return ohtml;
}

public Boolean getSectionNaturalIds() {
return sni;
}
}
5 changes: 4 additions & 1 deletion src/main/java/ru/curs/asciidoctor_idgen/Extender.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ class Extender {
private String jsonFilePath;
private String copyPath;
private ArrayList<ExtendedBlock> allBlocks;
private Boolean sectionNaturalIds;

private ArrayList<String> lines; //список строк файла
private int shift = 0; //количество новых строк, которое было вставлено в документ, относительно исходного

Extender(String path, String outPath, String jsonFilePath, ArrayList<ExtendedBlock> allBlocks) throws IOException {
Extender(String path, String outPath, String jsonFilePath,
ArrayList<ExtendedBlock> allBlocks, Boolean sectionNaturalIds) throws IOException {
this.outPath = outPath;
this.jsonFilePath = jsonFilePath;

this.allBlocks = allBlocks;
this.sectionNaturalIds = sectionNaturalIds;

this.lines = new ArrayList<>();

Expand Down
Loading