Skip to content

Commit

Permalink
Deeply traverse markup include:: sections
Browse files Browse the repository at this point in the history
  • Loading branch information
rahmanusta committed Mar 20, 2015
1 parent 14859b1 commit e50aae8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 99 deletions.
183 changes: 98 additions & 85 deletions src/main/java/com/kodcu/service/convert/DocBookService.java
Expand Up @@ -24,7 +24,7 @@
public class DocBookService { public class DocBookService {


private final Pattern ascIncludeRegex = Pattern.compile("(?<=include::)(?<path>.*?)(?=\\[(.*?)\\])"); private final Pattern ascIncludeRegex = Pattern.compile("(?<=include::)(?<path>.*?)(?=\\[(.*?)\\])");
private final Pattern mdIncludeRegex = Pattern.compile("\\[.*?\\]\\((?<path>.*?)\\)"); private final Pattern mdIncludeRegex = Pattern.compile("\\[.*?\\]\\((?<path>.*\\.(md|markdown|asc|adoc|asciidoc|ad|txt))\\)");


private final RenderService docConverter; private final RenderService docConverter;
private final Current current; private final Current current;
Expand All @@ -35,107 +35,120 @@ public DocBookService(final RenderService docConverter, final Current current) {
this.current = current; this.current = current;
} }


public void generateDocbook(Consumer<String> step) { private void traverseLines(List<String> lines, StringBuffer buffer, Path rootPath) {

for (String line : lines) {

Matcher ascMatcher = ascIncludeRegex.matcher(line);
Matcher markdownMatcher = mdIncludeRegex.matcher(line);

if (ascMatcher.find()) {
String chapterPath = ascMatcher.group("path");
Path chapterFile = rootPath.resolve(chapterPath);
String chapterContent = IOHelper.readFile(chapterFile);
traverseLines(Arrays.asList(chapterContent.split("\\r?\\n")), buffer, chapterFile.getParent());
} else if (markdownMatcher.find()) {
String chapterPath = markdownMatcher.group("path");
Path chapterFile = rootPath.resolve(chapterPath);
String chapterContent = IOHelper.readFile(chapterFile);
traverseLines(Arrays.asList(chapterContent.split("\\r?\\n")), buffer, chapterFile.getParent());
} else
traverseLine(line, buffer);
}
}

private void traverseLine(String line, StringBuffer buffer) {
if (line.matches("^=+ +.*:.*"))
line = line.replace(":", "00HEADER00COLON00");
buffer.append(line + "\n");
}


StringBuilder builder = new StringBuilder(); public void generateDocbook(Consumer<String> step) {


Path currentTabPath = current.currentPath().get(); StringBuffer outputBuffer = new StringBuffer();
Path currentTabPathDir = currentTabPath.getParent();
String tabText = current.getCurrentTabText().replace("*", "").trim();


List<String> bookAscLines = Arrays.asList(current.currentEditorValue().split("\\r?\\n")); Path currentTabPath = current.currentPath().get();
for (int i = 0; i < bookAscLines.size(); i++) { Path currentTabPathDir = currentTabPath.getParent();
String bookAscLine = bookAscLines.get(i);


Matcher matcher = ascIncludeRegex.matcher(bookAscLine); StringBuffer stringBuffer = new StringBuffer();


if (matcher.find()) { traverseLines(Arrays.asList(current.currentEditorValue().split("\\r?\\n")), stringBuffer, currentTabPathDir);
String chapterPath = matcher.group("path");
String chapterContent = IOHelper.readFile(currentTabPathDir.resolve(chapterPath));
bookAscLines.set(i, "\n\n" + chapterContent + "\n\n");
}


if(tabText.contains("SUMMARY")){ String text = stringBuffer.toString();
matcher = mdIncludeRegex.matcher(bookAscLine);


if (matcher.find()) { docConverter.convertDocbook(text, true, docBookHeaderContent -> {
String chapterPath = matcher.group("path"); docBookHeaderContent = docBookHeaderContent.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
String chapterContent = IOHelper.readFile(currentTabPathDir.resolve(chapterPath));
bookAscLines.set(i, "\n\n" + chapterContent + "\n\n");
}
}


} StringReader bookReader = new StringReader(docBookHeaderContent);
Match rootDocument = IOHelper.$(new InputSource(bookReader));
bookReader.close();


StringBuffer allAscContent = new StringBuffer(); // // makes figure centering
bookAscLines.forEach(content -> { rootDocument.find("figure").find("imagedata").attr("align", "center");
allAscContent.append(content);
allAscContent.append("\n"); // remove callout's duplicated refs and pick last
rootDocument.find("callout").forEach(elem -> {
String arearefs = $(elem).attr("arearefs");
String[] cos = arearefs.split(" ");
if (cos.length > 1)
$(elem).attr("arearefs", cos[cos.length - 1]);
}); });


String text = allAscContent.toString(); outputBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outputBuffer.append("\n");
outputBuffer.append("<?asciidoc-toc?>");
outputBuffer.append("\n");
outputBuffer.append("<?asciidoc-numbered?>");
outputBuffer.append("\n");
outputBuffer.append(rootDocument.content());
String result = outputBuffer.toString();
result = result.replace("00HEADER00COLON00", ":");
step.accept(result);
});
}

public void generateDocbookArticle(Consumer<String> step) {


docConverter.convertDocbook(text, true,docBookHeaderContent->{ StringBuilder outputBuffer = new StringBuilder();
docBookHeaderContent = docBookHeaderContent.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","<?xml version=\"1.1\" encoding=\"UTF-8\"?>"); Path currentTabPath = current.currentPath().get();
Path currentTabPathDir = currentTabPath.getParent();


StringReader bookReader = new StringReader(docBookHeaderContent); StringBuffer stringBuffer = new StringBuffer();
Match rootDocument = IOHelper.$(new InputSource(bookReader));
bookReader.close();


// // makes figure centering // traverseLines(Arrays.asList(current.currentEditorValue().split("\\r?\\n")), stringBuffer, currentTabPathDir);
rootDocument.find("figure").find("imagedata").attr("align", "center"); // String input = stringBuffer.toString();


// remove callout's duplicated refs and pick last docConverter.convertDocbookArticle(current.currentEditorValue(), docbook -> {
rootDocument.find("callout").forEach(elem -> {
String arearefs = $(elem).attr("arearefs"); docbook = docbook.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.1\" encoding=\"UTF-8\"?>");
String[] cos = arearefs.split(" "); StringReader bookReader = new StringReader(docbook);
if (cos.length > 1) Match rootDocument = IOHelper.$(new InputSource(bookReader));
$(elem).attr("arearefs", cos[cos.length - 1]); bookReader.close();
});

// changes formalpara to figure bug fix
builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); rootDocument.find("imageobject").parents("formalpara").each((context) -> {
builder.append("\n"); $(context).rename("figure");
builder.append("<?asciidoc-toc?>");
builder.append("\n");
builder.append("<?asciidoc-numbered?>");
builder.append("\n");
builder.append(rootDocument.content());
step.accept(builder.toString());
}); });
}


public void generateDocbookArticle(Consumer<String> step) { // makes figure centering
rootDocument.find("figure").find("imagedata").attr("align", "center");


docConverter.convertDocbookArticle(docbook -> { // remove callout's duplicated refs and pick last
StringBuilder builder = new StringBuilder(); rootDocument.find("callout").forEach(elem -> {
docbook = docbook.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<?xml version=\"1.1\" encoding=\"UTF-8\"?>"); String arearefs = $(elem).attr("arearefs");
StringReader bookReader = new StringReader(docbook); String[] cos = arearefs.split(" ");
Match rootDocument = IOHelper.$(new InputSource(bookReader)); if (cos.length > 1)
bookReader.close(); $(elem).attr("arearefs", cos[cos.length - 1]);

// changes formalpara to figure bug fix
rootDocument.find("imageobject").parents("formalpara").each((context) -> {
$(context).rename("figure");
});

// makes figure centering
rootDocument.find("figure").find("imagedata").attr("align", "center");

// remove callout's duplicated refs and pick last
rootDocument.find("callout").forEach(elem -> {
String arearefs = $(elem).attr("arearefs");
String[] cos = arearefs.split(" ");
if (cos.length > 1)
$(elem).attr("arearefs", cos[cos.length - 1]);
});


builder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
builder.append("\n");
builder.append("<?asciidoc-toc?>\n");
builder.append("<?asciidoc-numbered?>\n");
builder.append(rootDocument.content());
step.accept(builder.toString());
}); });

outputBuffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outputBuffer.append("\n");
outputBuffer.append("<?asciidoc-toc?>\n");
outputBuffer.append("<?asciidoc-numbered?>\n");
outputBuffer.append(rootDocument.content());
String result = outputBuffer.toString();
result = result.replace("00HEADER00COLON00", ":");
step.accept(result);
});
} }
} }
4 changes: 1 addition & 3 deletions src/main/java/com/kodcu/service/convert/RenderService.java
Expand Up @@ -89,16 +89,14 @@ public void convertDocbook(String input, boolean includeHeader, Consumer<String>
}); });
} }


public void convertDocbookArticle(Consumer<String> step) { public void convertDocbookArticle(String input,Consumer<String> step) {


String input = current.currentEditorValue();
markdownService.convert(input, asciidoc -> { markdownService.convert(input, asciidoc -> {
threadService.runActionLater(() -> { threadService.runActionLater(() -> {
getWindow().setMember("editorValue", asciidoc); getWindow().setMember("editorValue", asciidoc);
String rendered = execute(controller.getPreviewView(), "convertDocbookArticle(editorValue)"); String rendered = execute(controller.getPreviewView(), "convertDocbookArticle(editorValue)");
step.accept(rendered); step.accept(rendered);
}); });
}); });

} }
} }
20 changes: 15 additions & 5 deletions src/main/java/com/kodcu/service/ui/WebviewService.java
Expand Up @@ -5,8 +5,10 @@
import com.kodcu.other.Current; import com.kodcu.other.Current;
import com.kodcu.service.*; import com.kodcu.service.*;
import com.kodcu.service.extension.AsciiTreeGenerator; import com.kodcu.service.extension.AsciiTreeGenerator;
import com.kodcu.service.shortcut.ShortcutProvider;
import javafx.scene.control.ContextMenu; import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem; import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.input.Dragboard; import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseButton; import javafx.scene.input.MouseButton;
import javafx.scene.web.WebEngine; import javafx.scene.web.WebEngine;
Expand Down Expand Up @@ -35,17 +37,19 @@ public class WebviewService {


private Optional<DocumentService> documentService = Optional.empty(); private Optional<DocumentService> documentService = Optional.empty();
private final AsciiTreeGenerator asciiTreeGenerator; private final AsciiTreeGenerator asciiTreeGenerator;
private final ShortcutProvider shortcutProvider;


@Autowired @Autowired
public WebviewService(final ApplicationController controller, final PathResolverService pathResolver, final ThreadService threadService, public WebviewService(final ApplicationController controller, final PathResolverService pathResolver, final ThreadService threadService,
final ParserService parserService, final MarkdownService markdownService, final Current current, AsciiTreeGenerator asciiTreeGenerator) { final ParserService parserService, final MarkdownService markdownService, final Current current, AsciiTreeGenerator asciiTreeGenerator, ShortcutProvider shortcutProvider) {
this.controller = controller; this.controller = controller;
this.pathResolver = pathResolver; this.pathResolver = pathResolver;
this.threadService = threadService; this.threadService = threadService;
this.parserService = parserService; this.parserService = parserService;
this.markdownService = markdownService; this.markdownService = markdownService;
this.current = current; this.current = current;
this.asciiTreeGenerator = asciiTreeGenerator; this.asciiTreeGenerator = asciiTreeGenerator;
this.shortcutProvider = shortcutProvider;
} }


public void setDocumentService(DocumentService documentService) { public void setDocumentService(DocumentService documentService) {
Expand All @@ -69,7 +73,10 @@ public WebView createWebView() {
MenuItem pasteRaw = MenuItemBuilt.item("Paste raw").click(e -> { MenuItem pasteRaw = MenuItemBuilt.item("Paste raw").click(e -> {
current.insertEditorValue(controller.pasteRaw()); current.insertEditorValue(controller.pasteRaw());
}); });
MenuItem convert = MenuItemBuilt.item("Markdown to Asciidoc").click(e -> { MenuItem indexSelection = MenuItemBuilt.item("Index selection").click(e -> {
shortcutProvider.getProvider().addIndexSelection();
});
MenuItem markdownToAsciidoc = MenuItemBuilt.item("Markdown to Asciidoc").click(e -> {
markdownService.convertToAsciidoc(current.currentEditorValue(), markdownService.convertToAsciidoc(current.currentEditorValue(),
content -> { content -> {
threadService.runActionLater(() -> { threadService.runActionLater(() -> {
Expand All @@ -81,15 +88,18 @@ public WebView createWebView() {
webView.setOnMouseClicked(event -> { webView.setOnMouseClicked(event -> {


if (menu.getItems().size() == 0) { if (menu.getItems().size() == 0) {
menu.getItems().addAll(copy, paste, pasteRaw, convert); menu.getItems().addAll(copy, paste, pasteRaw,
markdownToAsciidoc,
indexSelection
);
} }


if (menu.isShowing()) { if (menu.isShowing()) {
menu.hide(); menu.hide();
} }
if (event.getButton() == MouseButton.SECONDARY) { if (event.getButton() == MouseButton.SECONDARY) {
boolean markdown = current.currentTab().isMarkdown(); markdownToAsciidoc.setVisible(current.currentTab().isMarkdown());
convert.setVisible(markdown); indexSelection.setVisible(current.currentTab().isAsciidoc());
menu.show(webView, event.getScreenX(), event.getScreenY()); menu.show(webView, event.getScreenX(), event.getScreenY());
} }
}); });
Expand Down
19 changes: 13 additions & 6 deletions src/main/resources/public/js/editor-shortcuts.js
Expand Up @@ -181,29 +181,36 @@ var editorMenu = {
var session = editor.getSession(); var session = editor.getSession();
session.insert(cursorPosition, "* "); session.insert(cursorPosition, "* ");
}, },
addAdmonition : function(type){ addAdmonition: function (type) {
var range = editor.getSelectionRange(); var range = editor.getSelectionRange();
editor.removeToLineStart(); editor.removeToLineStart();
editor.insert("["+type+"]\n====\n\n===="); editor.insert("[" + type + "]\n====\n\n====");
editor.gotoLine(range.end.row + 3, 0, true); editor.gotoLine(range.end.row + 3, 0, true);
}, },
addSidebarBlock:function(){ addSidebarBlock: function () {
var range = editor.getSelectionRange(); var range = editor.getSelectionRange();
editor.removeToLineStart(); editor.removeToLineStart();
editor.insert(".Title\n****\n\n****"); editor.insert(".Title\n****\n\n****");
editor.gotoLine(range.end.row + 3, 0, true); editor.gotoLine(range.end.row + 3, 0, true);
}, },
addExampleBlock:function(){ addExampleBlock: function () {
var range = editor.getSelectionRange(); var range = editor.getSelectionRange();
editor.removeToLineStart(); editor.removeToLineStart();
editor.insert(".Title\n====\n\n===="); editor.insert(".Title\n====\n\n====");
editor.gotoLine(range.end.row + 3, 0, true); editor.gotoLine(range.end.row + 3, 0, true);
}, },
addPassthroughBlock: function(){ addPassthroughBlock: function () {
var range = editor.getSelectionRange(); var range = editor.getSelectionRange();
editor.removeToLineStart(); editor.removeToLineStart();
editor.insert("++++\n\n++++"); editor.insert("++++\n\n++++");
editor.gotoLine(range.end.row + 2, 0, true); editor.gotoLine(range.end.row + 2, 0, true);
},
addIndexSelection: function () {
var range = editor.getSelectionRange();
var selectedText = editor.session.getTextRange(range);
if (selectedText)
if (selectedText.trim() != "")
editor.insert("(((" + selectedText + ")))" + selectedText);
} }
}, },
markdown: { markdown: {
Expand Down Expand Up @@ -335,7 +342,7 @@ editor.commands.addCommand({
editor.commands.addCommand({ editor.commands.addCommand({
name: 'highlight-selected', name: 'highlight-selected',
bindKey: {win: 'Ctrl-H', mac: 'Command-H'}, bindKey: {win: 'Ctrl-H', mac: 'Command-H'},
exec: function(){ exec: function () {
app.getShortcutProvider().getProvider().addHighlight(); app.getShortcutProvider().getProvider().addHighlight();
}, },
readOnly: true readOnly: true
Expand Down

0 comments on commit e50aae8

Please sign in to comment.