Skip to content

Commit

Permalink
Deduplicate functions for including JS/CSS assets
Browse files Browse the repository at this point in the history
  • Loading branch information
Fs00 authored and maniac103 committed Nov 29, 2021
1 parent 1230091 commit ba7ae43
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 40 deletions.
5 changes: 3 additions & 2 deletions app/src/main/java/com/gh4a/activities/DiffViewerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.gh4a.utils.ActivityResultHelpers;
import com.gh4a.utils.ApiHelpers;
import com.gh4a.utils.FileUtils;
import com.gh4a.utils.HtmlUtils;
import com.gh4a.utils.IntentUtils;
import com.gh4a.utils.RxUtils;
import com.gh4a.utils.StringUtils;
Expand Down Expand Up @@ -239,8 +240,8 @@ protected String generateHtml(String cssTheme, boolean addTitleHeader) {
content.append(title);
}
content.append("</title>");
writeCssInclude(content, "text", cssTheme);
writeScriptInclude(content, "codeutils");
HtmlUtils.writeCssInclude(content, "text", cssTheme);
HtmlUtils.writeScriptInclude(content, "codeutils");
content.append("</head><body");

int highlightInsertPos = content.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.gh4a.ServiceFactory;
import com.gh4a.utils.ApiHelpers;
import com.gh4a.utils.FileUtils;
import com.gh4a.utils.HtmlUtils;
import com.gh4a.utils.IntentUtils;
import com.gh4a.utils.Optional;
import com.gh4a.utils.StringUtils;
Expand Down Expand Up @@ -328,7 +329,7 @@ private void openUnsuitableFileAndFinish() {
private static String highlightImage(String imageUrl, String cssTheme, String title) {
StringBuilder content = new StringBuilder();
content.append("<html><head>");
writeCssInclude(content, "text", cssTheme);
HtmlUtils.writeCssInclude(content, "text", cssTheme);
content.append("</head><body>");
if (title != null) {
content.append("<h2>").append(title).append("</h2>");
Expand Down
26 changes: 6 additions & 20 deletions app/src/main/java/com/gh4a/activities/WebViewerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ protected String generateMarkdownHtml(String base64Data,
content.append(title);
}
content.append("</title>");
writeScriptInclude(content, "showdown");
writeCssInclude(content, "markdown", cssTheme);
HtmlUtils.writeScriptInclude(content, "showdown");
HtmlUtils.writeCssInclude(content, "markdown", cssTheme);
content.append("</head>");

content.append("<body>");
Expand Down Expand Up @@ -408,13 +408,13 @@ protected String generateCodeHtml(String data, String fileName,
content.append(title);
}
content.append("</title>");
writeScriptInclude(content, "codeutils");
HtmlUtils.writeScriptInclude(content, "codeutils");

writeCssInclude(content, "prettify", cssTheme);
writeScriptInclude(content, "prettify");
HtmlUtils.writeCssInclude(content, "prettify", cssTheme);
HtmlUtils.writeScriptInclude(content, "prettify");
loadLanguagePluginListIfNeeded();
for (String plugin : sLanguagePlugins) {
writeScriptInclude(content, plugin);
HtmlUtils.writeScriptInclude(content, plugin);
}
content.append("</head>");
content.append("<body onload='prettyPrint(function() { highlightLines(");
Expand Down Expand Up @@ -444,20 +444,6 @@ protected static String wrapUnthemedHtml(String html, String cssTheme, String ti
return style + "<body>" + titleHeader + html + "</body>";
}

protected static void writeScriptInclude(StringBuilder builder, String scriptName) {
builder.append("<script src='file:///android_asset/");
builder.append(scriptName);
builder.append(".js' type='text/javascript'></script>");
}

protected static void writeCssInclude(StringBuilder builder, String cssType, String cssTheme) {
builder.append("<link href='file:///android_asset/");
builder.append(cssType);
builder.append("-");
builder.append(cssTheme);
builder.append(".css' rel='stylesheet' type='text/css'/>");
}

@Override
protected abstract boolean canSwipeToRefresh();

Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/gh4a/utils/HtmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@
import static android.graphics.Paint.Style.FILL;

public class HtmlUtils {
public static void writeScriptInclude(StringBuilder builder, String scriptName) {
builder.append("<script src='file:///android_asset/");
builder.append(scriptName);
builder.append(".js' type='text/javascript'></script>");
}

public static void writeCssInclude(StringBuilder builder, String cssType, String cssTheme) {
builder.append("<link href='file:///android_asset/");
builder.append(cssType);
builder.append("-");
builder.append(cssTheme);
builder.append(".css' rel='stylesheet' type='text/css'/>");
}

private static class ReplySpan implements LeadingMarginSpan {
private final int mColor;
private final int mMargin;
Expand Down
21 changes: 4 additions & 17 deletions app/src/main/java/com/gh4a/widget/MarkdownPreviewWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import com.gh4a.R;
import com.gh4a.activities.WebViewerActivity;
import com.gh4a.utils.HtmlUtils;
import com.gh4a.utils.StringUtils;

public class MarkdownPreviewWebView extends WebView implements NestedScrollingChild2 {
Expand Down Expand Up @@ -177,9 +178,9 @@ private void setContent(String content) {
private String generateMarkdownHtml(String base64Data, String cssTheme) {
StringBuilder content = new StringBuilder();
content.append("<html><head>");
writeScriptInclude(content, "showdown");
writeCssInclude(content, "markdown", cssTheme);
writeCssInclude(content, "mdpreview", cssTheme);
HtmlUtils.writeScriptInclude(content, "showdown");
HtmlUtils.writeCssInclude(content, "markdown", cssTheme);
HtmlUtils.writeCssInclude(content, "mdpreview", cssTheme);
content.append("</head>");

content.append("<body>");
Expand All @@ -201,20 +202,6 @@ private String generateMarkdownHtml(String base64Data, String cssTheme) {
return content.toString();
}

private static void writeScriptInclude(StringBuilder builder, String scriptName) {
builder.append("<script src='file:///android_asset/");
builder.append(scriptName);
builder.append(".js' type='text/javascript'></script>");
}

private static void writeCssInclude(StringBuilder builder, String cssType, String cssTheme) {
builder.append("<link href='file:///android_asset/");
builder.append(cssType);
builder.append("-");
builder.append(cssTheme);
builder.append(".css' rel='stylesheet' type='text/css'/>");
}

private static class Base64JavascriptInterface {
@JavascriptInterface
public String decode(String base64) {
Expand Down

0 comments on commit ba7ae43

Please # to comment.