LatexPlugin now depens on inline-parser plugin
This commit is contained in:
parent
74682ae605
commit
1c08e3f240
@ -16,11 +16,10 @@ android {
|
|||||||
dependencies {
|
dependencies {
|
||||||
|
|
||||||
api project(':markwon-core')
|
api project(':markwon-core')
|
||||||
|
api project(':markwon-inline-parser')
|
||||||
|
|
||||||
api deps['jlatexmath-android']
|
api deps['jlatexmath-android']
|
||||||
|
|
||||||
debugImplementation project(':markwon-inline-parser')
|
|
||||||
|
|
||||||
deps['test'].with {
|
deps['test'].with {
|
||||||
testImplementation it['junit']
|
testImplementation it['junit']
|
||||||
testImplementation it['robolectric']
|
testImplementation it['robolectric']
|
||||||
|
@ -14,7 +14,6 @@ import androidx.annotation.Nullable;
|
|||||||
import androidx.annotation.Px;
|
import androidx.annotation.Px;
|
||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import org.commonmark.parser.InlineParserFactory;
|
|
||||||
import org.commonmark.parser.Parser;
|
import org.commonmark.parser.Parser;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -31,7 +30,7 @@ import io.noties.markwon.image.AsyncDrawableLoader;
|
|||||||
import io.noties.markwon.image.AsyncDrawableScheduler;
|
import io.noties.markwon.image.AsyncDrawableScheduler;
|
||||||
import io.noties.markwon.image.AsyncDrawableSpan;
|
import io.noties.markwon.image.AsyncDrawableSpan;
|
||||||
import io.noties.markwon.image.ImageSizeResolver;
|
import io.noties.markwon.image.ImageSizeResolver;
|
||||||
import io.noties.markwon.inlineparser.MarkwonInlineParser;
|
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin;
|
||||||
import ru.noties.jlatexmath.JLatexMathDrawable;
|
import ru.noties.jlatexmath.JLatexMathDrawable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -153,10 +152,19 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
|||||||
this.inlineImageSizeResolver = new InlineImageSizeResolver();
|
this.inlineImageSizeResolver = new InlineImageSizeResolver();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(@NonNull Registry registry) {
|
||||||
|
if (RenderMode.BLOCKS_AND_INLINES == config.renderMode) {
|
||||||
|
registry.require(MarkwonInlineParserPlugin.class)
|
||||||
|
.factoryBuilder()
|
||||||
|
.addInlineProcessor(new JLatexMathInlineProcessor());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void configureParser(@NonNull Parser.Builder builder) {
|
public void configureParser(@NonNull Parser.Builder builder) {
|
||||||
|
|
||||||
// TODO: depending on renderMode we should register our parsing here
|
// depending on renderMode we should register our parsing here
|
||||||
// * for LEGACY -> just add custom block parser
|
// * for LEGACY -> just add custom block parser
|
||||||
// * for INLINE.. -> require InlinePlugin, add inline processor + add block parser
|
// * for INLINE.. -> require InlinePlugin, add inline processor + add block parser
|
||||||
|
|
||||||
@ -169,11 +177,7 @@ public class JLatexMathPlugin extends AbstractMarkwonPlugin {
|
|||||||
|
|
||||||
case BLOCKS_AND_INLINES: {
|
case BLOCKS_AND_INLINES: {
|
||||||
builder.customBlockParserFactory(new JLatexMathBlockParser.Factory());
|
builder.customBlockParserFactory(new JLatexMathBlockParser.Factory());
|
||||||
|
// inline processor is added through `registry`
|
||||||
final InlineParserFactory factory = MarkwonInlineParser.factoryBuilder()
|
|
||||||
.addInlineProcessor(new JLatexMathInlineProcessor())
|
|
||||||
.build();
|
|
||||||
builder.inlineParserFactory(factory);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -11,12 +11,41 @@ import androidx.annotation.Nullable;
|
|||||||
import io.noties.markwon.Markwon;
|
import io.noties.markwon.Markwon;
|
||||||
import io.noties.markwon.ext.latex.JLatexMathPlugin;
|
import io.noties.markwon.ext.latex.JLatexMathPlugin;
|
||||||
import io.noties.markwon.ext.latex.JLatexMathTheme;
|
import io.noties.markwon.ext.latex.JLatexMathTheme;
|
||||||
|
import io.noties.markwon.inlineparser.MarkwonInlineParserPlugin;
|
||||||
import io.noties.markwon.sample.ActivityWithMenuOptions;
|
import io.noties.markwon.sample.ActivityWithMenuOptions;
|
||||||
import io.noties.markwon.sample.MenuOptions;
|
import io.noties.markwon.sample.MenuOptions;
|
||||||
import io.noties.markwon.sample.R;
|
import io.noties.markwon.sample.R;
|
||||||
|
|
||||||
public class LatexActivity extends ActivityWithMenuOptions {
|
public class LatexActivity extends ActivityWithMenuOptions {
|
||||||
|
|
||||||
|
private static final String LATEX_ARRAY;
|
||||||
|
|
||||||
|
static {
|
||||||
|
String latex = "\\begin{array}{l}";
|
||||||
|
latex += "\\forall\\varepsilon\\in\\mathbb{R}_+^*\\ \\exists\\eta>0\\ |x-x_0|\\leq\\eta\\Longrightarrow|f(x)-f(x_0)|\\leq\\varepsilon\\\\";
|
||||||
|
latex += "\\det\\begin{bmatrix}a_{11}&a_{12}&\\cdots&a_{1n}\\\\a_{21}&\\ddots&&\\vdots\\\\\\vdots&&\\ddots&\\vdots\\\\a_{n1}&\\cdots&\\cdots&a_{nn}\\end{bmatrix}\\overset{\\mathrm{def}}{=}\\sum_{\\sigma\\in\\mathfrak{S}_n}\\varepsilon(\\sigma)\\prod_{k=1}^n a_{k\\sigma(k)}\\\\";
|
||||||
|
latex += "\\sideset{_\\alpha^\\beta}{_\\gamma^\\delta}{\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}}\\\\";
|
||||||
|
latex += "\\int_0^\\infty{x^{2n} e^{-a x^2}\\,dx} = \\frac{2n-1}{2a} \\int_0^\\infty{x^{2(n-1)} e^{-a x^2}\\,dx} = \\frac{(2n-1)!!}{2^{n+1}} \\sqrt{\\frac{\\pi}{a^{2n+1}}}\\\\";
|
||||||
|
latex += "\\int_a^b{f(x)\\,dx} = (b - a) \\sum\\limits_{n = 1}^\\infty {\\sum\\limits_{m = 1}^{2^n - 1} {\\left( { - 1} \\right)^{m + 1} } } 2^{ - n} f(a + m\\left( {b - a} \\right)2^{-n} )\\\\";
|
||||||
|
latex += "\\int_{-\\pi}^{\\pi} \\sin(\\alpha x) \\sin^n(\\beta x) dx = \\textstyle{\\left \\{ \\begin{array}{cc} (-1)^{(n+1)/2} (-1)^m \\frac{2 \\pi}{2^n} \\binom{n}{m} & n \\mbox{ odd},\\ \\alpha = \\beta (2m-n) \\\\ 0 & \\mbox{otherwise} \\\\ \\end{array} \\right .}\\\\";
|
||||||
|
latex += "L = \\int_a^b \\sqrt{ \\left|\\sum_{i,j=1}^ng_{ij}(\\gamma(t))\\left(\\frac{d}{dt}x^i\\circ\\gamma(t)\\right)\\left(\\frac{d}{dt}x^j\\circ\\gamma(t)\\right)\\right|}\\,dt\\\\";
|
||||||
|
latex += "\\begin{array}{rl} s &= \\int_a^b\\left\\|\\frac{d}{dt}\\vec{r}\\,(u(t),v(t))\\right\\|\\,dt \\\\ &= \\int_a^b \\sqrt{u'(t)^2\\,\\vec{r}_u\\cdot\\vec{r}_u + 2u'(t)v'(t)\\, \\vec{r}_u\\cdot\\vec{r}_v+ v'(t)^2\\,\\vec{r}_v\\cdot\\vec{r}_v}\\,\\,\\, dt. \\end{array}\\\\";
|
||||||
|
latex += "\\end{array}";
|
||||||
|
LATEX_ARRAY = latex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final String LATEX_LONG_DIVISION = "\\text{A long division \\longdiv{12345}{13}";
|
||||||
|
private static final String LATEX_BANGLE = "{a \\bangle b} {c \\brace d} {e \\brack f} {g \\choose h}";
|
||||||
|
private static final String LATEX_BOXES;
|
||||||
|
|
||||||
|
static {
|
||||||
|
String latex = "\\begin{array}{cc}";
|
||||||
|
latex += "\\fbox{\\text{A framed box with \\textdbend}}&\\shadowbox{\\text{A shadowed box}}\\cr";
|
||||||
|
latex += "\\doublebox{\\text{A double framed box}}&\\ovalbox{\\text{An oval framed box}}\\cr";
|
||||||
|
latex += "\\end{array}";
|
||||||
|
LATEX_BOXES = latex;
|
||||||
|
}
|
||||||
|
|
||||||
private TextView textView;
|
private TextView textView;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ -27,7 +56,8 @@ public class LatexActivity extends ActivityWithMenuOptions {
|
|||||||
.add("longDivision", this::longDivision)
|
.add("longDivision", this::longDivision)
|
||||||
.add("bangle", this::bangle)
|
.add("bangle", this::bangle)
|
||||||
.add("boxes", this::boxes)
|
.add("boxes", this::boxes)
|
||||||
.add("insideBlockQuote", this::insideBlockQuote);
|
.add("insideBlockQuote", this::insideBlockQuote)
|
||||||
|
.add("legacy", this::legacy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -42,36 +72,19 @@ public class LatexActivity extends ActivityWithMenuOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void array() {
|
private void array() {
|
||||||
String latex = "\\begin{array}{l}";
|
render(wrapLatexInSampleMarkdown(LATEX_ARRAY));
|
||||||
latex += "\\forall\\varepsilon\\in\\mathbb{R}_+^*\\ \\exists\\eta>0\\ |x-x_0|\\leq\\eta\\Longrightarrow|f(x)-f(x_0)|\\leq\\varepsilon\\\\";
|
|
||||||
latex += "\\det\\begin{bmatrix}a_{11}&a_{12}&\\cdots&a_{1n}\\\\a_{21}&\\ddots&&\\vdots\\\\\\vdots&&\\ddots&\\vdots\\\\a_{n1}&\\cdots&\\cdots&a_{nn}\\end{bmatrix}\\overset{\\mathrm{def}}{=}\\sum_{\\sigma\\in\\mathfrak{S}_n}\\varepsilon(\\sigma)\\prod_{k=1}^n a_{k\\sigma(k)}\\\\";
|
|
||||||
latex += "\\sideset{_\\alpha^\\beta}{_\\gamma^\\delta}{\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}}\\\\";
|
|
||||||
latex += "\\int_0^\\infty{x^{2n} e^{-a x^2}\\,dx} = \\frac{2n-1}{2a} \\int_0^\\infty{x^{2(n-1)} e^{-a x^2}\\,dx} = \\frac{(2n-1)!!}{2^{n+1}} \\sqrt{\\frac{\\pi}{a^{2n+1}}}\\\\";
|
|
||||||
latex += "\\int_a^b{f(x)\\,dx} = (b - a) \\sum\\limits_{n = 1}^\\infty {\\sum\\limits_{m = 1}^{2^n - 1} {\\left( { - 1} \\right)^{m + 1} } } 2^{ - n} f(a + m\\left( {b - a} \\right)2^{-n} )\\\\";
|
|
||||||
latex += "\\int_{-\\pi}^{\\pi} \\sin(\\alpha x) \\sin^n(\\beta x) dx = \\textstyle{\\left \\{ \\begin{array}{cc} (-1)^{(n+1)/2} (-1)^m \\frac{2 \\pi}{2^n} \\binom{n}{m} & n \\mbox{ odd},\\ \\alpha = \\beta (2m-n) \\\\ 0 & \\mbox{otherwise} \\\\ \\end{array} \\right .}\\\\";
|
|
||||||
latex += "L = \\int_a^b \\sqrt{ \\left|\\sum_{i,j=1}^ng_{ij}(\\gamma(t))\\left(\\frac{d}{dt}x^i\\circ\\gamma(t)\\right)\\left(\\frac{d}{dt}x^j\\circ\\gamma(t)\\right)\\right|}\\,dt\\\\";
|
|
||||||
latex += "\\begin{array}{rl} s &= \\int_a^b\\left\\|\\frac{d}{dt}\\vec{r}\\,(u(t),v(t))\\right\\|\\,dt \\\\ &= \\int_a^b \\sqrt{u'(t)^2\\,\\vec{r}_u\\cdot\\vec{r}_u + 2u'(t)v'(t)\\, \\vec{r}_u\\cdot\\vec{r}_v+ v'(t)^2\\,\\vec{r}_v\\cdot\\vec{r}_v}\\,\\,\\, dt. \\end{array}\\\\";
|
|
||||||
latex += "\\end{array}";
|
|
||||||
|
|
||||||
render(wrapLatexInSampleMarkdown(latex));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void longDivision() {
|
private void longDivision() {
|
||||||
String latex = "\\text{A long division \\longdiv{12345}{13}";
|
render(wrapLatexInSampleMarkdown(LATEX_LONG_DIVISION));
|
||||||
render(wrapLatexInSampleMarkdown(latex));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void bangle() {
|
private void bangle() {
|
||||||
String latex = "{a \\bangle b} {c \\brace d} {e \\brack f} {g \\choose h}";
|
render(wrapLatexInSampleMarkdown(LATEX_BANGLE));
|
||||||
render(wrapLatexInSampleMarkdown(latex));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void boxes() {
|
private void boxes() {
|
||||||
String latex = "\\begin{array}{cc}";
|
render(wrapLatexInSampleMarkdown(LATEX_BOXES));
|
||||||
latex += "\\fbox{\\text{A framed box with \\textdbend}}&\\shadowbox{\\text{A shadowed box}}\\cr";
|
|
||||||
latex += "\\doublebox{\\text{A double framed box}}&\\ovalbox{\\text{An oval framed box}}\\cr";
|
|
||||||
latex += "\\end{array}";
|
|
||||||
render(wrapLatexInSampleMarkdown(latex));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insideBlockQuote() {
|
private void insideBlockQuote() {
|
||||||
@ -82,6 +95,22 @@ public class LatexActivity extends ActivityWithMenuOptions {
|
|||||||
render(md);
|
render(md);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void legacy() {
|
||||||
|
final String md = wrapLatexInSampleMarkdown(LATEX_BANGLE);
|
||||||
|
|
||||||
|
final Markwon markwon = Markwon.builder(this)
|
||||||
|
// LEGACY does not require inline parser
|
||||||
|
.usePlugin(JLatexMathPlugin.create(textView.getTextSize(), builder -> {
|
||||||
|
builder.renderMode(JLatexMathPlugin.RenderMode.LEGACY);
|
||||||
|
builder.theme()
|
||||||
|
.backgroundProvider(() -> new ColorDrawable(0x100000ff))
|
||||||
|
.padding(JLatexMathTheme.Padding.all(48));
|
||||||
|
}))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
markwon.setMarkdown(textView, md);
|
||||||
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private static String wrapLatexInSampleMarkdown(@NonNull String latex) {
|
private static String wrapLatexInSampleMarkdown(@NonNull String latex) {
|
||||||
return "" +
|
return "" +
|
||||||
@ -99,6 +128,8 @@ public class LatexActivity extends ActivityWithMenuOptions {
|
|||||||
final Resources r = getResources();
|
final Resources r = getResources();
|
||||||
|
|
||||||
final Markwon markwon = Markwon.builder(this)
|
final Markwon markwon = Markwon.builder(this)
|
||||||
|
// NB! `MarkwonInlineParserPlugin` is required in order to parse inlines
|
||||||
|
.usePlugin(MarkwonInlineParserPlugin.create())
|
||||||
.usePlugin(JLatexMathPlugin.create(textSize, textSize * 1.25F, builder -> {
|
.usePlugin(JLatexMathPlugin.create(textSize, textSize * 1.25F, builder -> {
|
||||||
builder.theme()
|
builder.theme()
|
||||||
.inlineBackgroundProvider(() -> new ColorDrawable(0x1000ff00))
|
.inlineBackgroundProvider(() -> new ColorDrawable(0x1000ff00))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user