Skip to content
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

Support AsciiDoc View-Mode by TextConverter with asciidoctor.js #2022

Merged
merged 8 commits into from
May 11, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,54 @@
#########################################################*/
package net.gsantner.markor.format.asciidoc;

import net.gsantner.markor.format.plaintext.PlaintextTextConverter;
import android.content.Context;
import net.gsantner.markor.format.TextConverterBase;
import net.gsantner.opoc.format.GsTextUtils;

import java.util.ArrayList;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

@SuppressWarnings("WeakerAccess")
public class AsciidocTextConverter extends PlaintextTextConverter {
public class AsciidocTextConverter extends TextConverterBase {
//########################
//## Extensions
//########################
private static final Set<String> EXT = new HashSet<>(Arrays.asList(".adoc", ".asciidoc", ".asc"));
public static final String HTML_ASCIIDOCJS_JS_INCLUDE = "<script src='file:///android_asset/asciidoc/asciidoctor.min.js'></script>";
public static final String HTML_ASCIIDOCJS_DEFAULT_CSS_INCLUDE = "file:///android_asset/asciidoc/asciidoctor.css";
/**
* that file was loaded from <a href="https://github.com/darshandsoni/asciidoctor-skins/blob/gh-pages/css/dark.css">dark.css</a>
* "import" block was changed to load local css
* "literalblock" block was changes to support new rules
*/
public static final String HTML_ASCIIDOCJS_DARK_CSS_INCLUDE = "file:///android_asset/asciidoc/dark.css";

private static final List<String> EXT_ASCIIDOC = Arrays.asList(".adoc", ".asciidoc", ".asc");
private static final List<String> EXT = new ArrayList<>();

static {
EXT.addAll(EXT_ASCIIDOC);
@Override
public String convertMarkup(String markup, Context context, boolean isExportInLightMode, File file) {
String converted = "<div id=\"asciidoc_content\"></div>\n";
String onLoadJs = "var textBase64 = `" +
//convert a text to base64 to simplify supporting special characters
GsTextUtils.toBase64(markup) +
"`;\n" +
//decode base64 to utf8 string
"const asciiPlainText = atob(textBase64);\n" +
"const length = asciiPlainText.length;\n" +
"const bytes = new Uint8Array(length);\n" +
"for (let i = 0; i < length; i++) {\n" +
" bytes[i] = asciiPlainText.charCodeAt(i);\n" +
"}\n" +
"const decoder = new TextDecoder();\n" +
"var utf8PlainText = decoder.decode(bytes);" +
"var asciidoctor = Asciidoctor();\n" +
//standalone : true - to generate header 1 (= title) in the page. if don't do that - title will be absent.
//nofooter: true - to don't generate footer (Last updated ...). if don't do that and use standalone : true - the page will have that footer.
"var html = asciidoctor.convert(utf8PlainText, {standalone : true, attributes : {nofooter: true, stylesheet: \"" +
(isExportInLightMode ? HTML_ASCIIDOCJS_DEFAULT_CSS_INCLUDE : HTML_ASCIIDOCJS_DARK_CSS_INCLUDE)
+ "\"}});\n" +
"document.getElementById(\"asciidoc_content\").innerHTML = html;";
return putContentIntoTemplate(context, converted, isExportInLightMode, file, onLoadJs, HTML_ASCIIDOCJS_JS_INCLUDE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ private CharSequence autoIndent(final CharSequence source, final CharSequence de

final OrderedListLine oLine = new OrderedListLine(dest, dstart, _patterns);
final UnOrderedOrCheckListLine uLine = new UnOrderedOrCheckListLine(dest, dstart, _patterns);
final String indent = source + oLine.line.substring(0, oLine.indent); // Copy indent from previous line
final String indent;
if (oLine.indent < 0 || oLine.indent > oLine.line.length()) {
indent = source.toString();
} else {
// Copy indent from previous line
indent = source + oLine.line.substring(0, oLine.indent);
}

final String result;
if (oLine.isOrderedList && oLine.lineEnd != oLine.groupEnd && dend >= oLine.groupEnd) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/raw/licenses_3rd_party.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
* Mermaid
~° Copyright Mermaid authors

* asciidoctor.js
~° Copyright asciidoctor.js authors

asciidoctor-skins
~° Copyright asciidoctor-skins authors

`---------------`
<small>Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
Loading