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

Reworking line numbers + other fixes #2088

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ab04552
add line numbers support
Jun 5, 2023
33b3d78
fix bug in calculating width of max line number
Jun 6, 2023
af10f41
remove 3 strings of non english translations, update licenses_3rd_par…
Jun 6, 2023
a4c7161
Bugfix: Mermaid code cannot be rendered when line numbers enabled
Jun 18, 2023
488f476
Optimized code
Jun 23, 2023
3dd82a4
Bugfix: Line numbers cannot behave well with bigger headings enabled
Jun 23, 2023
e5069a5
perf(line numbers): drawing line numbers only near the visible area
Jun 27, 2023
a0579cc
fix(line numbers): ClassCastException when disable line wrapping
Jun 27, 2023
289cfb8
rework line number settings
gsantner Jul 9, 2023
e118cd0
automatic code reformat, add license notice for flexmark extension
gsantner Jul 9, 2023
d480f9a
supress lint due checked with getLineCount
gsantner Jul 9, 2023
40077ac
todo.txt improvement
gsantner Jul 9, 2023
a7fdd30
feat(line numbers): align line numbers text to the right; minor perfo…
Jul 15, 2023
bc83846
refactor(line numbers): minor improvements
Jul 17, 2023
dbe456b
perf(line numbers): performance improvements
Jul 17, 2023
57d5935
refactor(line numbers): minor improvements
Jul 19, 2023
4cbee78
refactor(line numbers): minor improvements
Jul 20, 2023
8d62124
Token for inserting selection
harshad1 Aug 4, 2023
7ecb268
Ignoring token in new file dialog
harshad1 Aug 4, 2023
35692bf
Tweaks
harshad1 Aug 4, 2023
c899628
refactor(line numbers): minor improvements
Aug 6, 2023
4900610
Merge remote-tracking branch 'coworker/line_numbers_support' into loc…
harshad1 Aug 6, 2023
b75fdfb
perf(line numbers): added support for iterating from the first visibl…
Aug 8, 2023
7901b7f
Merge remote-tracking branch 'guang-lin/line_numbers_support' into lo…
harshad1 Aug 11, 2023
dc1c9f1
Reworked line numbers for some simplicity
harshad1 Aug 11, 2023
892cdf4
Fixes, per file
harshad1 Aug 11, 2023
7ae90c2
Converter with line num support
harshad1 Aug 12, 2023
7faa0db
GetChars made faster
harshad1 Aug 12, 2023
c7b0cd7
Many small tweaks
harshad1 Aug 13, 2023
8f18b61
Unrelated fix for matching directories too
harshad1 Aug 13, 2023
d38bef6
Rework single pass line numbering
harshad1 Aug 14, 2023
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 @@ -200,6 +200,7 @@ public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
_hlEditor.setTextColor(_appSettings.getEditorForegroundColor());
_hlEditor.setGravity(_appSettings.isEditorStartEditingInCenter() ? Gravity.CENTER : Gravity.NO_GRAVITY);
_hlEditor.setHighlightingEnabled(_appSettings.getDocumentHighlightState(_document.getPath(), _hlEditor.getText()));
_hlEditor.setLineNumbersEnabled(_appSettings.getDocumentLineNumbersEnabled(_document.getPath()));
_hlEditor.setAutoFormatEnabled(_appSettings.getDocumentAutoFormatEnabled(_document.getPath()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Do not need to send contents to accessibility
Expand Down Expand Up @@ -458,7 +459,7 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
if (saveDocument(false)) {
TextConverterBase converter = FormatRegistry.getFormat(_document.getFormat(), activity, _document).getConverter();
_cu.shareText(getActivity(),
converter.convertMarkup(getTextString(), getActivity(), false, _document.getFile()),
converter.convertMarkup(getTextString(), getActivity(), false, _hlEditor.getLineNumbersEnabled(), _document.getFile()),
"text/" + (item.getItemId() == R.id.action_share_html ? "html" : "plain")
);
}
Expand Down Expand Up @@ -561,6 +562,13 @@ public void onFsViewerConfig(GsFileBrowserOptions.Options dopt) {
updateMenuToggleStates(0);
return true;
}
case R.id.action_line_numbers: {
final boolean newState = !_hlEditor.getLineNumbersEnabled();
_appSettings.setDocumentLineNumbersEnabled(_document.getPath(), newState);
_hlEditor.setLineNumbersEnabled(newState);
updateMenuToggleStates(0);
return true;
}
case R.id.action_enable_highlighting: {
final boolean newState = !_hlEditor.getHighlightingEnabled();
_hlEditor.setHighlightingEnabled(newState);
Expand Down Expand Up @@ -626,6 +634,9 @@ private void updateMenuToggleStates(final int selectedFormatActionId) {
if ((mi = _fragmentMenu.findItem(R.id.action_enable_highlighting)) != null) {
mi.setChecked(_hlEditor.getHighlightingEnabled());
}
if ((mi = _fragmentMenu.findItem(R.id.action_line_numbers)) != null) {
mi.setChecked(_hlEditor.getLineNumbersEnabled());
}
if ((mi = _fragmentMenu.findItem(R.id.action_enable_auto_format)) != null) {
mi.setChecked(_hlEditor.getAutoFormatEnabled());
}
Expand Down Expand Up @@ -747,7 +758,7 @@ private boolean isDisplayedAtMainActivity() {

public void updateViewModeText() {
final String text = getTextString();
_format.getConverter().convertMarkupShowInWebView(_document, text, getActivity(), _webView, _nextConvertToPrintMode);
_format.getConverter().convertMarkupShowInWebView(_document, text, getActivity(), _webView, _nextConvertToPrintMode, _hlEditor.getLineNumbersEnabled());
}

public void setViewModeVisibility(boolean show) {
Expand Down
31 changes: 13 additions & 18 deletions app/src/main/java/net/gsantner/markor/format/TextConverterBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.gsantner.markor.model.Document;
import net.gsantner.opoc.format.GsTextUtils;
import net.gsantner.opoc.util.GsContextUtils;
import net.gsantner.opoc.util.GsFileUtils;

import java.io.File;
import java.util.Date;
Expand Down Expand Up @@ -97,19 +98,18 @@ public TextConverterBase() {
* @param webView The WebView content to be shown in
* @return Copy of converted html
*/
public String convertMarkupShowInWebView(Document document, String content, Activity context, WebView webView, boolean isExportInLightMode) {
public String convertMarkupShowInWebView(Document document, String content, Activity context, WebView webView, boolean lightMode, boolean lineNum) {
String html;
try {
html = convertMarkup(content, context, isExportInLightMode, document.getFile());
html = convertMarkup(content, context, lightMode, lineNum, document.getFile());
} catch (Exception e) {
html = "Please report at project issue tracker: " + e.toString();
html = "Please report at project issue tracker: " + e;
}

String baseFolder = ApplicationObject.settings().getNotebookDirectory().getAbsolutePath();
if (document.getFile().getParentFile() != null) {
baseFolder = document.getFile().getParent();
String baseFolder = document.getFile().getParent();
if (baseFolder == null) {
baseFolder = "file://" + baseFolder + "/";
}
baseFolder = "file://" + baseFolder + "/";
webView.loadDataWithBaseURL(baseFolder, html, getContentType(), UTF_CHARSET, null);

// When TOKEN_TEXT_CONVERTER_MAX_ZOOM_OUT_BY_DEFAULT is contained in text zoom out as far possible
Expand All @@ -121,21 +121,16 @@ public String convertMarkupShowInWebView(Document document, String content, Acti
return html;
}

protected String getFileExtension(File file) {
if (file == null) {
return "";
}
return (file.getName().contains(".") ? file.getName().substring(file.getName().lastIndexOf(".")) : "").toLowerCase();
}

/**
* Convert markup text to target format
*
* @param markup Markup text
* @param context Android Context
* @param markup Markup text
* @param context Android Context
* @param lightMode
* @param lineNum
* @return html as String
*/
public abstract String convertMarkup(String markup, Context context, boolean isExportInLightMode, File file);
public abstract String convertMarkup(String markup, Context context, boolean lightMode, boolean lineNum, File file);

protected String putContentIntoTemplate(Context context, String content, boolean isExportInLightMode, File file, String onLoadJs, String head) {
final String contentLower = content.toLowerCase();
Expand Down Expand Up @@ -185,7 +180,7 @@ protected String putContentIntoTemplate(Context context, String content, boolean
.replace(TOKEN_ACCENT_COLOR, GsTextUtils.colorToHexString(ContextCompat.getColor(context, R.color.accent)))
.replace(TOKEN_TEXT_DIRECTION, _appSettings.isRenderRtl() ? "right" : "left")
.replace(TOKEN_FONT, font)
.replace(TOKEN_TEXT_CONVERTER_CSS_CLASS, "format-" + getClass().getSimpleName().toLowerCase().replace("textconverter", "").replace("converter", "") + " fileext-" + getFileExtension(file).replace(".", ""))
.replace(TOKEN_TEXT_CONVERTER_CSS_CLASS, "format-" + getClass().getSimpleName().toLowerCase().replace("textconverter", "").replace("converter", "") + " fileext-" + GsFileUtils.getFilenameExtension(file).replace(".", ""))
.replace(TOKEN_POST_TODAY_DATE, DateFormat.getDateFormat(context).format(new Date()))
.replace(TOKEN_FILEURI_VIEWED_FILE, (file != null ? Uri.fromFile(file.getAbsoluteFile()).toString() : "file:///dummy").replace("'", "\\'").replace("\"", "\\\""));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package net.gsantner.markor.format.asciidoc;

import android.content.Context;

import net.gsantner.markor.format.TextConverterBase;
import net.gsantner.opoc.format.GsTextUtils;

Expand All @@ -32,7 +33,7 @@ public class AsciidocTextConverter extends TextConverterBase {
public static final String HTML_ASCIIDOCJS_DARK_CSS_INCLUDE = "file:///android_asset/asciidoc/dark.css";

@Override
public String convertMarkup(String markup, Context context, boolean isExportInLightMode, File file) {
public String convertMarkup(String markup, Context context, boolean lightMode, boolean lineNum, File file) {
String converted = "<div id=\"asciidoc_content\"></div>\n";
String onLoadJs = "var textBase64 = `" +
//convert a text to base64 to simplify supporting special characters
Expand All @@ -51,10 +52,10 @@ public String convertMarkup(String markup, Context context, boolean isExportInLi
//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)
(lightMode ? 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);
return putContentIntoTemplate(context, converted, lightMode, file, onLoadJs, HTML_ASCIIDOCJS_JS_INCLUDE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class EmbedBinaryTextConverter extends TextConverterBase {

@SuppressWarnings({"ConstantConditions", "StringConcatenationInLoop"})
@Override
public String convertMarkup(String markup, Context context, boolean isExportInLightMode, File file) {
public String convertMarkup(String markup, Context context, boolean lightMode, boolean lineNum, File file) {
String converted = "", onLoadJs = "", head = "";
if (file == null) {
return "";
Expand Down Expand Up @@ -152,7 +152,7 @@ public String convertMarkup(String markup, Context context, boolean isExportInLi
}

converted += HTML101_BODY_END;
return putContentIntoTemplate(context, converted, isExportInLightMode, file, onLoadJs, head);
return putContentIntoTemplate(context, converted, lightMode, file, onLoadJs, head);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.opencsv.CSVReaderBuilder;
import com.opencsv.ICSVParser;

import net.gsantner.markor.format.TextConverterBase;
import net.gsantner.markor.format.markdown.MarkdownTextConverter;

import java.io.BufferedReader;
Expand All @@ -33,17 +34,17 @@
* Part of Markor-Architecture implementing Preview/Export for csv.
* <p>
* Converts csv to md and let
* {@link MarkdownTextConverter#convertMarkup(String, Context, boolean, File)}
* {@link TextConverterBase#convertMarkup(String, Context, boolean, boolean, File)}
* do the rest.
* <p>
* This way csv columns may contain md expressions like bold text.
*/
@SuppressWarnings("WeakerAccess")
public class CsvTextConverter extends MarkdownTextConverter {
@Override
public String convertMarkup(String csvMarkup, Context context, boolean isExportInLightMode, File file) {
public String convertMarkup(String csvMarkup, Context context, boolean lightMode, boolean lineNum, File file) {
String mdMarkup = Csv2MdTable.toMdTable(csvMarkup);
return super.convertMarkup(mdMarkup, context, isExportInLightMode, file);
return super.convertMarkup(mdMarkup, context, lightMode, lineNum, file);
}

@Override
Expand Down Expand Up @@ -133,7 +134,8 @@ private static void addColumnsLine(StringBuilder mdMarkup, @NonNull String[] col
mdMarkup.append(MD_COL_DELIMITER).append(MD_LINE_DELIMITER);
}

@NonNull private static String getCol(@NonNull String[] columns, int i) {
@NonNull
private static String getCol(@NonNull String[] columns, int i) {
return (i >= 0 && i < columns.length) ? columns[i] : "";
}

Expand Down
Loading
Loading