Merge pull request #1123 from kekkyojin/style-code-tag
Styling <code> tag for Markdown content
This commit is contained in:
commit
f0f0a5028b
3 changed files with 35 additions and 1 deletions
|
@ -40,6 +40,7 @@ import androidx.annotation.NonNull;
|
|||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.widget.AppCompatSpinner;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.text.HtmlCompat;
|
||||
import androidx.core.widget.NestedScrollView;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
@ -76,8 +77,12 @@ import com.google.android.material.button.MaterialButton;
|
|||
import com.google.android.material.snackbar.Snackbar;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
|
||||
import org.commonmark.node.Code;
|
||||
import org.commonmark.node.Node;
|
||||
import org.commonmark.parser.Parser;
|
||||
import org.commonmark.renderer.html.AttributeProvider;
|
||||
import org.commonmark.renderer.html.AttributeProviderContext;
|
||||
import org.commonmark.renderer.html.AttributeProviderFactory;
|
||||
import org.commonmark.renderer.html.HtmlRenderer;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
@ -2194,7 +2199,14 @@ public class FileViewFragment extends BaseFragment implements
|
|||
private String buildMarkdownHtml(String markdown) {
|
||||
Parser parser = Parser.builder().build();
|
||||
Node document = parser.parse(markdown);
|
||||
HtmlRenderer renderer = HtmlRenderer.builder().build();
|
||||
HtmlRenderer renderer = HtmlRenderer.builder()
|
||||
.attributeProviderFactory(new AttributeProviderFactory() {
|
||||
@Override
|
||||
public AttributeProvider create(AttributeProviderContext context) {
|
||||
return new CodeAttributeProvider();
|
||||
}
|
||||
})
|
||||
.build();
|
||||
String markdownHtml = renderer.render(document);
|
||||
|
||||
return "<!doctype html>\n" +
|
||||
|
@ -3235,4 +3247,20 @@ public class FileViewFragment extends BaseFragment implements
|
|||
});
|
||||
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
class CodeAttributeProvider implements AttributeProvider {
|
||||
|
||||
@Override
|
||||
public void setAttributes(Node node, String tagName, Map<String, String> attributes) {
|
||||
if (node instanceof Code) {
|
||||
Context context = getContext();
|
||||
String colorCodeText = "#".concat(Integer.toHexString(ContextCompat.getColor(context, R.color.codeTagText) & 0x00ffffff));
|
||||
String colorCodeBg = "#".concat(Integer.toHexString(ContextCompat.getColor(context, R.color.codeTagBackground) & 0x00ffffff));
|
||||
String codeStyle = "display: inline-block; border-radius: 0.2rem" +
|
||||
"; color: " + colorCodeText + "; background-color: " + colorCodeBg +
|
||||
"; font-size: 0.8571rem; padding: calc(2rem/5 - 4px) calc(2rem/5) calc(2rem/5 - 5px);";
|
||||
attributes.put("style", codeStyle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -58,4 +58,7 @@
|
|||
|
||||
<color name="caption">#CAEDB9</color>
|
||||
<color name="overlay">#CC333333</color>
|
||||
|
||||
<color name="codeTagText">#E8B692</color>
|
||||
<color name="codeTagBackground">#464B50</color>
|
||||
</resources>
|
|
@ -58,4 +58,7 @@
|
|||
|
||||
<color name="caption">#CAEDB9</color>
|
||||
<color name="overlay">#CC333333</color>
|
||||
|
||||
<color name="codeTagText">#2E3439</color>
|
||||
<color name="codeTagBackground">#D0E8FF</color>
|
||||
</resources>
|
Loading…
Reference in a new issue