1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

Xtn.Math: Swap out math tags with UNIQ

This commit is contained in:
gnosygnu
2016-12-01 19:55:43 -05:00
parent 2a0b5769ad
commit 1e28aa6e79
12 changed files with 86 additions and 34 deletions

View File

@@ -20,6 +20,7 @@ import gplx.core.brys.fmtrs.*;
import gplx.xowa.htmls.*; import gplx.langs.htmls.entitys.*; import gplx.xowa.htmls.core.htmls.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.vnts.*;
public class Xof_math_html_wtr {
public static final byte[] Bry__id_prefix = Bry_.new_a7("xowa_math_txt_");
private final Bry_fmtr math_fmtr_latex = Bry_fmtr.new_("<img id='xowa_math_img_~{math_idx}' src='' width='' height=''/><span id='xowa_math_txt_~{math_idx}'>~{math_text}</span>", "math_idx", "math_text");
private final Bry_fmtr math_fmtr_mathjax = Bry_fmtr.new_("<span id='xowa_math_txt_~{math_idx}'>~{math_text}</span>", "math_idx", "math_text");
public void Write(Xoh_html_wtr wtr, Xop_ctx ctx, Xoh_wtr_ctx opts, Bry_bfr bfr, byte[] src, Xop_xnde_tkn xnde) {
@@ -45,12 +46,16 @@ public class Xof_math_html_wtr {
private void Write_for_mathjax(Bry_bfr bfr, Xoae_page page, boolean enabled, boolean renderer_is_latex, byte[] math_bry, Bry_bfr tmp_bfr, Xof_math_itm math_itm) {
int id = page.File_math().Count();
Xof_math_itm new_math_itm = math_itm.Clone().Id_(id);
// boolean armor_math = page.Lang().Vnt_mgr().Enabled() && !renderer_is_latex; // REF.MW:LangConverter.php|armourMath
// if (armor_math) bfr.Add(Vnt_convert_lang.Bry__armor_bgn);
// bfr.Add_bfr_and_clear(tmp_bfr);
// if (armor_math) bfr.Add(Vnt_convert_lang.Bry__armor_end);
byte[] unique_bry = page.Wikie().Parser_mgr().Uniq_mgr().Add(math_bry);
Bry_fmtr math_fmtr = renderer_is_latex ? math_fmtr_latex : math_fmtr_mathjax;
boolean armor_math = page.Lang().Vnt_mgr().Enabled() && !renderer_is_latex; // REF.MW:LangConverter.php|armourMath
if (armor_math) bfr.Add(Vnt_convert_lang.Bry__armor_bgn);
math_fmtr.Bld_bfr_many(tmp_bfr, id, math_bry);
math_fmtr.Bld_bfr_many(tmp_bfr, id, unique_bry);
bfr.Add_bfr_and_clear(tmp_bfr);
if (armor_math) bfr.Add(Vnt_convert_lang.Bry__armor_end);
if (enabled && renderer_is_latex) // NOTE: only generate images if math is enabled; otherwise "downloading" prints at bottom of screen, but no action (also a lot of file IO)
page.File_math().Add(new_math_itm);
}

View File

@@ -18,24 +18,33 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.xtns.math; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Xof_math_mgr_html_tst {
@Before public void init() {} private final Xop_fxt fxt = new Xop_fxt();
@Test public void Escape_lt_gt_mathjax() { // PURPOSE: escape <>&"; EX:de.w:Vergleich_(Zahlen); DATE:2014-05-10; PAGE:s.w:Matrix_(mathematics) DATE:2014-07-19
fxt.Test_html_full_str("<math>a<>b</math>", "<span id='xowa_math_txt_0'>a&lt;&gt;b</span>");
}
@Test public void Escape_lt_gt_latex() {
@Before public void init() {} private final Xop_fxt fxt = Xop_fxt.New_app_html();
@Test public void Basic__latex() {
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(false);
fxt.Test_html_full_str("<math>a<>b</math>", "<img id='xowa_math_img_0' src='' width='' height=''/><span id='xowa_math_txt_0'>a<>b</span>");
fxt.Test__parse_to_html_mgr("<math>x + y</math>", "<img id='xowa_math_img_0' src='' width='' height=''/><span id='xowa_math_txt_0'>x + y</span>"); // latex has img
}
@Test public void Basic__mathjax() {
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
fxt.Test__parse_to_html_mgr("<math>x + y</math>", "<span id='xowa_math_txt_0'>x + y</span>"); // mathjax has no img
}
@Test public void Escape__mathjax() { // PURPOSE: escape <>&"; EX:de.w:Vergleich_(Zahlen); DATE:2014-05-10; PAGE:s.w:Matrix_(mathematics) DATE:2014-07-19
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
fxt.Test__parse_to_html_mgr("<math>a<>b</math>", "<span id='xowa_math_txt_0'>a&lt;&gt;b</span>");
}
@Test public void Escape__latex() {
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(false);
fxt.Test__parse_to_html_mgr("<math>a<>b</math>", "<img id='xowa_math_img_0' src='' width='' height=''/><span id='xowa_math_txt_0'>a<>b</span>");
}
@Test public void Amp() { // PURPOSE: assert that amp is not escaped; DATE:2014-07-20
fxt.Test_html_full_str("<math>a&b</math>", "<span id='xowa_math_txt_0'>a&b</span>");
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
fxt.Test__parse_to_html_mgr("<math>a&b</math>", "<span id='xowa_math_txt_0'>a&b</span>");
}
@Test public void Quote() { // PURPOSE: assert that quote is not escaped; DATE:2014-07-20
fxt.Test_html_full_str("<math>a\"b</math>", "<span id='xowa_math_txt_0'>a\"b</span>");
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
fxt.Test__parse_to_html_mgr("<math>a\"b</math>", "<span id='xowa_math_txt_0'>a\"b</span>");
}
@Test public void Vnt() { // PURPOSE: if vnt, armor math; PAGE:sr.w:Земља; DATE:2015-10-11
fxt.Wiki().Lang().Vnt_mgr().Enabled_(Bool_.Y);
fxt.Test_html_full_str("<math>abc</math>", "-{R|<span id='xowa_math_txt_0'>abc</span>}-"); // NOTE: this also armors the id which is good for the mathjax.js
fxt.Wiki().Lang().Vnt_mgr().Enabled_(Bool_.N);
@Test public void Script() {
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(false);
fxt.Test__parse_to_html_mgr("<math><script>alert('fail');</script></math>", "<img id='xowa_math_img_0' src='' width='' height=''/><span id='xowa_math_txt_0'>&lt;script>alert('fail');</script></span>");
}
}