mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.2.1
This commit is contained in:
31
400_xowa/src/gplx/xowa/xtns/math/Math_nde.java
Normal file
31
400_xowa/src/gplx/xowa/xtns/math/Math_nde.java
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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 gplx.xowa.html.*;
|
||||
public class Math_nde implements Xox_xnde, Xop_xnde_atr_parser {
|
||||
public Xop_xnde_tkn Xnde() {throw Exc_.new_unimplemented();}
|
||||
public void Xatr_parse(Xowe_wiki wiki, byte[] src, Xop_xatr_itm xatr, Object xatr_obj) {}
|
||||
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
|
||||
Xof_math_mgr math_mgr = wiki.Appe().File_mgr().Math_mgr();
|
||||
if (math_mgr.Enabled() && math_mgr.Renderer_is_mathjax())
|
||||
ctx.Cur_page().Html_data().Module_mgr().Itm__mathjax().Enabled_y_();
|
||||
}
|
||||
public void Xtn_write(Bry_bfr bfr, Xoae_app app, Xop_ctx ctx, Xoh_html_wtr html_wtr, Xoh_wtr_ctx hctx, Xop_xnde_tkn xnde, byte[] src) {
|
||||
app.File_mgr().Math_mgr().Html_wtr().Write(html_wtr, ctx, hctx, bfr, src, xnde);
|
||||
}
|
||||
}
|
||||
84
400_xowa/src/gplx/xowa/xtns/math/Xof_math_html_wtr.java
Normal file
84
400_xowa/src/gplx/xowa/xtns/math/Xof_math_html_wtr.java
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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 gplx.xowa.html.*;
|
||||
public class Xof_math_html_wtr {
|
||||
private Xof_math_itm tmp_math_itm = new Xof_math_itm();
|
||||
private 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 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) {
|
||||
Xoae_app app = ctx.App(); Xowe_wiki wiki = ctx.Wiki(); Xoae_page page = ctx.Cur_page();
|
||||
boolean renderer_is_latex = !app.File_mgr().Math_mgr().Renderer_is_mathjax();
|
||||
byte[] math_bry = Bry_.Mid(src, xnde.Tag_open_end(), xnde.Tag_close_bgn());
|
||||
Bry_bfr tmp_bfr = app.Utl__bfr_mkr().Get_b512().Mkr_rls();
|
||||
math_bry = Escape_tex(tmp_bfr, !renderer_is_latex, math_bry);
|
||||
byte[] math_bry_clean = app.Html_mgr().Js_cleaner().Clean(wiki, math_bry, 0, math_bry.length); // check for js;
|
||||
if (math_bry_clean != null) math_bry = math_bry_clean; // js found; use clean version; DATE:2013-08-26
|
||||
boolean enabled = app.File_mgr().Math_mgr().Enabled();
|
||||
if (renderer_is_latex && app.File_mgr().Math_mgr().Find_itm(tmp_math_itm, page.Wiki().Domain_str(), math_bry)) {
|
||||
bfr.Add(Xoh_consts.Img_bgn);
|
||||
bfr.Add_str(tmp_math_itm.Png_url().To_http_file_str());
|
||||
bfr.Add(Xoh_consts.__inline_quote);
|
||||
}
|
||||
else {
|
||||
int id = page.File_math().Count();
|
||||
Xof_math_itm new_math_itm = tmp_math_itm.Clone().Id_(id);
|
||||
Bry_fmtr math_fmtr = renderer_is_latex ? math_fmtr_latex : math_fmtr_mathjax;
|
||||
math_fmtr.Bld_bfr_many(tmp_bfr, id, math_bry);
|
||||
bfr.Add_bfr_and_clear(tmp_bfr);
|
||||
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);
|
||||
}
|
||||
}
|
||||
private static byte[] Escape_tex(Bry_bfr tmp_bfr, boolean mathjax, byte[] bry) {return Escape_tex(false, tmp_bfr, mathjax, bry, 0, bry.length);}
|
||||
private static byte[] Escape_tex(boolean write_to_bfr, Bry_bfr bfr, boolean mathjax, byte[] bry, int bgn, int end) {
|
||||
if (bry == null) return null;
|
||||
boolean dirty = write_to_bfr ? true : false; // if write_to_bfr, then mark true, else bfr.Add_mid(bry, 0, i); will write whole bry again
|
||||
byte[] escaped = null;
|
||||
for (int i = bgn; i < end; i++) {
|
||||
byte b = bry[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Lt: if (mathjax) escaped = gplx.html.Html_entity_.Lt_bry; break;
|
||||
case Byte_ascii.Gt: if (mathjax) escaped = gplx.html.Html_entity_.Gt_bry; break;
|
||||
// case Byte_ascii.Amp: escaped = Const_amp; break; // TOMBSTONE:never escape ampersand; PAGE:s.w:Matrix_(mathematics); DATE:2014-07-19
|
||||
// case Byte_ascii.Quote: if (mathjax) escaped = gplx.html.Html_entity_.Quote_bry; break; // TOMBSTONE:do not escape quote; PAGE:s.w:Matrix_(mathematics); DATE:2014-07-19
|
||||
default:
|
||||
if (dirty || write_to_bfr)
|
||||
bfr.Add_byte(b);
|
||||
continue;
|
||||
}
|
||||
// handle lt, gt, amp, quote; everything else handled by default: continue above
|
||||
if (escaped == null) { // handle do-not-escape calls; EX: Escape(y, y, n, y);
|
||||
if (dirty || write_to_bfr)
|
||||
bfr.Add_byte(b);
|
||||
}
|
||||
else {
|
||||
if (!dirty) {
|
||||
bfr.Add_mid(bry, 0, i);
|
||||
dirty = true;
|
||||
}
|
||||
bfr.Add(escaped);
|
||||
escaped = null;
|
||||
}
|
||||
}
|
||||
if (write_to_bfr)
|
||||
return null;
|
||||
else
|
||||
return dirty ? bfr.Xto_bry_and_clear() : bry;
|
||||
}
|
||||
}
|
||||
28
400_xowa/src/gplx/xowa/xtns/math/Xof_math_itm.java
Normal file
28
400_xowa/src/gplx/xowa/xtns/math/Xof_math_itm.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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 gplx.gfui.*;
|
||||
import gplx.xowa.apps.*;
|
||||
public class Xof_math_itm {
|
||||
public Xof_math_itm Ctor(byte[] math, String hash, Io_url png_url) {this.math = math; this.hash = hash; this.png_url = png_url; return this;}
|
||||
public int Id() {return id;} public Xof_math_itm Id_(int v) {id = v; return this;} private int id;
|
||||
public String Hash() {return hash;} private String hash;
|
||||
public byte[] Math() {return math;} private byte[] math;
|
||||
public Io_url Png_url() {return png_url;} Io_url png_url;
|
||||
public Xof_math_itm Clone() {return new Xof_math_itm().Ctor(math, hash, png_url);}
|
||||
}
|
||||
106
400_xowa/src/gplx/xowa/xtns/math/Xof_math_mgr.java
Normal file
106
400_xowa/src/gplx/xowa/xtns/math/Xof_math_mgr.java
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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 gplx.core.strings.*; import gplx.xowa.apps.progs.*;
|
||||
public class Xof_math_mgr implements GfoInvkAble {
|
||||
private Xoae_app app;
|
||||
public ProcessAdp Cmd_convert_tex_to_dvi() {return cmd_convert_tex_to_dvi;} private ProcessAdp cmd_convert_tex_to_dvi = new ProcessAdp();
|
||||
public ProcessAdp Cmd_convert_dvi_to_png() {return cmd_convert_dvi_to_png;} private ProcessAdp cmd_convert_dvi_to_png = new ProcessAdp();
|
||||
public void Init_by_app(Xoae_app app) {
|
||||
this.app = app;
|
||||
Xoa_prog_mgr app_mgr = app.Prog_mgr();
|
||||
cmd_convert_tex_to_dvi = app_mgr.App_convert_tex_to_dvi();
|
||||
cmd_convert_dvi_to_png = app_mgr.App_convert_dvi_to_png();
|
||||
}
|
||||
private Io_url Make_math_dir(String wiki_key) {return app.Fsys_mgr().Root_dir().GenSubDir_nest("file", wiki_key, "math");}
|
||||
public Xof_math_html_wtr Html_wtr() {return html_wtr;} private final Xof_math_html_wtr html_wtr = new Xof_math_html_wtr();
|
||||
public void Make_itm(Xof_math_itm rv, String wiki_key, byte[] math_bry) {
|
||||
Io_url math_dir = Make_math_dir(wiki_key);
|
||||
math_bry = app.Math_subst_regy().Subst(math_bry);
|
||||
String md5 = gplx.security.HashAlgo_.Md5.CalcHash(ConsoleDlg_.Null, gplx.ios.IoStream_.ary_(math_bry));
|
||||
Io_url png_fil = Make_png_fil(math_dir, md5);
|
||||
rv.Ctor(math_bry, md5, png_fil);
|
||||
}
|
||||
public boolean Find_itm(Xof_math_itm rv, String wiki_key, byte[] math_bry) {
|
||||
Make_itm(rv, wiki_key, math_bry);
|
||||
return Io_mgr.I.ExistsFil(rv.Png_url());
|
||||
}
|
||||
public boolean Renderer_is_mathjax() {return renderer_is_mathjax;} public void Renderer_is_mathjax_(boolean v) {renderer_is_mathjax = v;} private boolean renderer_is_mathjax = true;
|
||||
private Io_url Make_png_fil(Io_url math_dir, String hash) {
|
||||
String Math_dir_spr = math_dir.Info().DirSpr();
|
||||
tmp_sb.Clear().Add(math_dir.Raw())
|
||||
.Add(String_.CharAt(hash, 0)).Add(Math_dir_spr)
|
||||
.Add(String_.CharAt(hash, 1)).Add(Math_dir_spr)
|
||||
.Add(String_.CharAt(hash, 2)).Add(Math_dir_spr)
|
||||
.Add(hash).Add(".png")
|
||||
;
|
||||
return Io_url_.new_fil_(tmp_sb.Xto_str_and_clear());
|
||||
}
|
||||
public boolean MakePng(byte[] math, String hash, Io_url png_url, String prog_fmt) {
|
||||
if (!enabled) return false;
|
||||
Io_url tmp_dir = app.Usere().Fsys_mgr().App_temp_dir().GenSubDir("math"); // cmd_convert_tex_to_dvi.Tmp_dir();
|
||||
Io_url tex_url = tmp_dir.GenSubFil("xowa_math_temp.tex");
|
||||
String latex = Latex_wrap(math);
|
||||
prog_fmt = String_.Replace(prog_fmt, "~", "~~"); // double-up ~ or else will break in progress bar
|
||||
Io_mgr.I.SaveFilStr(tex_url, latex);
|
||||
cmd_convert_tex_to_dvi.Prog_fmt_(prog_fmt + " tex_to_dvi: ~{process_seconds} second(s); ~{process_exe_name} ~{process_exe_args}");
|
||||
boolean pass = cmd_convert_tex_to_dvi.Run(tex_url.Raw(), tmp_dir.Xto_api()).Exit_code_pass();
|
||||
if (!pass) {
|
||||
app.Usr_dlg().Warn_many(GRP_KEY, "tex_to_dvi.fail", "fail: tex_to_dvi: error=~{0} latex=~{1}", cmd_convert_tex_to_dvi.Rslt_out(), latex);
|
||||
}
|
||||
// NOTE: latex sometimes throws errors, but will generate .dvi; for sake of simplicity; always try to run dvipng
|
||||
Io_mgr.I.CreateDirIfAbsent(png_url.OwnerDir());
|
||||
cmd_convert_dvi_to_png.Prog_fmt_(prog_fmt + " dvi_to_png: ~{process_seconds} second(s); ~{process_exe_name} ~{process_exe_args}");
|
||||
pass = cmd_convert_dvi_to_png.Run(tex_url.GenNewExt(".dvi"), png_url, tmp_dir.Xto_api()).Exit_code_pass();
|
||||
if (!pass) {
|
||||
app.Usr_dlg().Warn_many(GRP_KEY, "dvi_to_png.fail", "fail: dvi_to_png: error=~{0} latex=~{1}", cmd_convert_tex_to_dvi.Rslt_out(), latex);
|
||||
}
|
||||
return pass;
|
||||
}
|
||||
private String_bldr tmp_sb = String_bldr_.new_();
|
||||
private String Latex_wrap(byte[] math) {return Latex_doc_fmtr.Bld_str_many(String_.Replace(String_.new_u8(math), "\n\n", "\n"));} // NOTE: remove lines that are completely blank; not sure if this is right; PAGE:en.w:Standard Model (mathematical formulation); <math>(\mathbf{1},\mathbf\n\n{1},0)</math>
|
||||
private static Bry_fmtr Latex_doc_fmtr = new Bry_fmtr()
|
||||
.Fmt_(String_.Concat_lines_nl_skip_last
|
||||
( "\\documentclass{article}"
|
||||
, "\\usepackage{amsmath}"
|
||||
, "\\usepackage{amsfonts}"
|
||||
, "\\usepackage{amssymb}"
|
||||
, "\\usepackage{color}"
|
||||
, "\\usepackage[landscape]{geometry}"
|
||||
, "\\pagestyle{empty}"
|
||||
, "\\begin{document}"
|
||||
, "\\begin{Large}"
|
||||
, "\\nonumber"
|
||||
, "$\\displaystyle"
|
||||
, "~{0}"
|
||||
, "$\\end{Large}"
|
||||
, "\\end{document}"
|
||||
));
|
||||
public boolean Enabled() {return enabled;} public Xof_math_mgr Enabled_(boolean v) {enabled = v; return this;} private boolean enabled = true;
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_enabled)) return Yn.Xto_str(enabled);
|
||||
else if (ctx.Match(k, Invk_enabled_)) enabled = m.ReadYn("v");
|
||||
else if (ctx.Match(k, Invk_renderer)) return renderer_is_mathjax ? "mathjax" : "latex";
|
||||
else if (ctx.Match(k, Invk_renderer_)) renderer_is_mathjax = String_.Eq(m.ReadStr("v"), "mathjax");
|
||||
else if (ctx.Match(k, Invk_renderer_list)) return Options_renderer_list;
|
||||
else return GfoInvkAble_.Rv_unhandled;
|
||||
return this;
|
||||
} private static final String Invk_enabled = "enabled", Invk_enabled_ = "enabled_", Invk_renderer = "renderer", Invk_renderer_ = "renderer_", Invk_renderer_list = "renderer_list";
|
||||
private static KeyVal[] Options_renderer_list = KeyVal_.Ary(KeyVal_.new_("mathjax", "MathJax"), KeyVal_.new_("latex", "LaTeX"));
|
||||
private static final String GRP_KEY = "xowa.math.mgr";
|
||||
}
|
||||
36
400_xowa/src/gplx/xowa/xtns/math/Xof_math_mgr_html_tst.java
Normal file
36
400_xowa/src/gplx/xowa/xtns/math/Xof_math_mgr_html_tst.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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 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<>b</span>");
|
||||
}
|
||||
@Test public void Escape_lt_gt_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.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
|
||||
}
|
||||
@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>");
|
||||
}
|
||||
@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>");
|
||||
}
|
||||
}
|
||||
27
400_xowa/src/gplx/xowa/xtns/math/Xof_math_mgr_tst.java
Normal file
27
400_xowa/src/gplx/xowa/xtns/math/Xof_math_mgr_tst.java
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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_tst {
|
||||
@Before public void init() {} Xof_math_subst_regy subst_regy = new Xof_math_subst_regy().Init();
|
||||
@Test public void Basic() {tst("a\\plusmn b" , "a\\pm b");}
|
||||
@Test public void Match_fails() {tst("a\\plusmna b" , "a\\plusmna b");}
|
||||
@Test public void Part() {tst("a\\part_t b" , "a\\partial_t b");} // PAGE:en.w:Faraday's law of induction
|
||||
@Test public void Partial() {tst("a\\partial_{x_i}" , "a\\partial_{x_i}");} // DEFECT: partial -> partialial
|
||||
private void tst(String src, String expd) {Tfds.Eq(expd, String_.new_u8(subst_regy.Subst(Bry_.new_u8(src))));}
|
||||
}
|
||||
176
400_xowa/src/gplx/xowa/xtns/math/Xof_math_subst_regy.java
Normal file
176
400_xowa/src/gplx/xowa/xtns/math/Xof_math_subst_regy.java
Normal file
@@ -0,0 +1,176 @@
|
||||
/*
|
||||
XOWA: the XOWA Offline Wiki Application
|
||||
Copyright (C) 2012 gnosygnu@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
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 gplx.core.btries.*;
|
||||
public class Xof_math_subst_regy {
|
||||
Bry_bfr bfr = Bry_bfr.new_();
|
||||
public byte[] Subst(byte[] src) {
|
||||
if (!init) Init();
|
||||
int src_len = src.length;
|
||||
int dollarSignCount = 0;
|
||||
for (int i = 0; i < src_len; i++) {
|
||||
byte b = src[i];
|
||||
Object o = trie.Match_bgn_w_byte(b, src, i, src_len);
|
||||
if (o == null)
|
||||
bfr.Add_byte(b);
|
||||
else {
|
||||
Xof_math_subst_itm itm = (Xof_math_subst_itm)o;
|
||||
int itm_src_len = itm.SrcLen();
|
||||
int nxt = i + itm_src_len;
|
||||
if (nxt < src_len) {
|
||||
switch (src[nxt]) { // NOTE: for now, manually list characters that are viable word end characters; NOTE: my knowledge of TeX is nil
|
||||
case Byte_ascii.Space:
|
||||
case Byte_ascii.Curly_end:
|
||||
case Byte_ascii.Brack_end:
|
||||
case Byte_ascii.Underline:
|
||||
case Byte_ascii.Nl: // NOTE: needed for \begin\n
|
||||
break;
|
||||
default:
|
||||
if (itm.WholeWord()) {
|
||||
bfr.Add_byte(b); // itm does not match; ignore; EX: \alpha is itm, but cur text is \alpham
|
||||
continue;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
bfr.Add(itm.Trg());
|
||||
if (itm.DollarSign()) ++dollarSignCount;
|
||||
i += itm_src_len - 1;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dollarSignCount; i++)
|
||||
bfr.Add_byte(Byte_ascii.Dollar);
|
||||
return bfr.Xto_bry_and_clear_and_trim();
|
||||
} boolean init = false;
|
||||
public Xof_math_subst_regy Init() {
|
||||
if (init) return this;
|
||||
init = true;
|
||||
Reg("\\Alpha", "\\mathrm{A}");
|
||||
Reg("\\Beta", "\\mathrm{B}");
|
||||
Reg("\\Epsilon", "\\mathrm{E}");
|
||||
Reg("\\Zeta", "\\mathrm{Z}");
|
||||
Reg("\\Eta", "\\mathrm{H}");
|
||||
Reg("\\thetasym", "\\vartheta");
|
||||
Reg("\\Iota", "\\mathrm{I}");
|
||||
Reg("\\Kappa", "\\mathrm{K}");
|
||||
Reg("\\Mu", "\\mathrm{M}");
|
||||
Reg("\\Nu", "\\mathrm{N}");
|
||||
Reg("\\omicron", "\\mathrm{o}");
|
||||
Reg("\\Omicron", "\\mathrm{O}");
|
||||
Reg("\\Rho", "\\mathrm{P}");
|
||||
Reg("\\Tau", "\\mathrm{T}");
|
||||
Reg("\\Chi", "\\mathrm{X}");
|
||||
Reg("\\alef", "\\aleph");
|
||||
Reg("\\alefsym", "\\aleph");
|
||||
Reg("\\larr", "\\leftarrow");
|
||||
Reg("\\rarr", "\\rightarrow");
|
||||
Reg("\\Larr", "\\Leftarrow");
|
||||
Reg("\\lArr", "\\Leftarrow");
|
||||
Reg("\\Rarr", "\\Rightarrow");
|
||||
Reg("\\rArr", "\\Rightarrow");
|
||||
Reg("\\uarr", "\\uparrow");
|
||||
Reg("\\uArr", "\\Uparrow");
|
||||
Reg("\\Uarr", "\\Uparrow");
|
||||
Reg("\\darr", "\\downarrow");
|
||||
Reg("\\dArr", "\\Downarrow");
|
||||
Reg("\\Darr", "\\Downarrow");
|
||||
Reg("\\lrarr", "\\leftrightarrow");
|
||||
Reg("\\harr", "\\leftrightarrow");
|
||||
Reg("\\Lrarr", "\\Leftrightarrow");
|
||||
Reg("\\Harr", "\\Leftrightarrow");
|
||||
Reg("\\lrArr", "\\Leftrightarrow");
|
||||
Reg("\\hAar", "\\Leftrightarrow");
|
||||
Reg("\\bull", "\\bullet");
|
||||
Reg("\\Dagger", "\\ddagger");
|
||||
Reg("\\weierp", "\\wp");
|
||||
Reg("\\and", "\\land");
|
||||
Reg("\\or", "\\lor");
|
||||
Reg("\\sub", "\\subset");
|
||||
Reg("\\supe", "\\supseteq");
|
||||
Reg("\\sube", "\\subseteq");
|
||||
Reg("\\infin", "\\infty");
|
||||
Reg("\\isin", "\\in");
|
||||
Reg("\\exist", "\\exists");
|
||||
Reg("\\ne", "\\neq");
|
||||
Reg("\\real", "\\Re");
|
||||
Reg("\\image", "\\Im");
|
||||
Reg("\\plusmn", "\\pm");
|
||||
Reg("\\sdot", "\\cdot");
|
||||
Reg("\\empty", "\\emptyset");
|
||||
Reg("\\O", "\\emptyset");
|
||||
Reg("\\sect", "\\S");
|
||||
Reg("\\ge", "\\geq");
|
||||
Reg("\\le", "\\leq");
|
||||
Reg("\\ang", "\\angle");
|
||||
Reg("\\part", "\\partial", false, true);
|
||||
Reg("\\lang", "\\langle");
|
||||
Reg("\\rang", "\\rangle");
|
||||
Reg("\\lbrack", "[");
|
||||
Reg("\\rbrack", "]");
|
||||
Reg("\\clubs", "\\clubsuit");
|
||||
Reg("\\spades", "\\spadesuit");
|
||||
Reg("\\hearts", "\\heartsuit");
|
||||
Reg("\\diamonds", "\\diamondsuit");
|
||||
Reg("\\euro", "\\mbox{\\euro}");
|
||||
Reg("\\geneuro", "\\mbox{\\geneuro}");
|
||||
Reg("\\geneuronarrow", "\\mbox{\\geneuronarrow}");
|
||||
Reg("\\geneurowide", "\\mbox{\\geneurowide}");
|
||||
Reg("\\officialeuro", "\\mbox{\\officialeuro}");
|
||||
Reg("\\Coppa", "\\mbox{\\Coppa}");
|
||||
Reg("\\coppa", "\\mbox{\\coppa}");
|
||||
Reg("\\varcoppa", "\\mbox{\\coppa}");
|
||||
Reg("\\Digamma", "\\mbox{\\Digamma}");
|
||||
Reg("\\Koppa", "\\mbox{\\Koppa}");
|
||||
Reg("\\koppa", "\\mbox{\\koppa}");
|
||||
Reg("\\Sampi", "\\mbox{\\Sampi}");
|
||||
Reg("\\sampi", "\\mbox{\\sampi}");
|
||||
Reg("\\Stigma", "\\mbox{\\Stigma}");
|
||||
Reg("\\stigma", "\\mbox{\\stigma}");
|
||||
Reg("\\varstigma", "\\mbox{\\varstigma}");
|
||||
Reg("\\reals", "\\mathbb{R}");
|
||||
Reg("\\Reals", "\\mathbb{R}");
|
||||
Reg("\\R", "\\mathbb{R}");
|
||||
Reg("\\C", "\\mathbb{C}");
|
||||
Reg("\\cnums", "\\mathbb{C}");
|
||||
Reg("\\Complex", "\\mathbb{C}");
|
||||
Reg("\\Z", "\\mathbb{Z}");
|
||||
Reg("\\natnums", "\\mathbb{N}");
|
||||
Reg("\\N", "\\mathbb{N}");
|
||||
Reg("\\Q", "\\mathbb{Q}");
|
||||
Reg("\\H", "\\mathbb{H}");
|
||||
Reg("\\restriction", "\\upharpoonright");
|
||||
Reg("\\begin{align}", "\n$\\begin{align}", true, true); // HACK: \begin{align} needs to have preceding blank line else "\begin{align} allowed only in paragraph mode." and no dvi produced
|
||||
return this;
|
||||
}
|
||||
private void Reg(String src_str, String trg_str) {Reg(src_str, trg_str, false, true);}
|
||||
private void Reg(String src_str, String trg_str, boolean dollarSign, boolean wholeWord) {
|
||||
byte[] src_bry = Bry_.new_a7(src_str);
|
||||
Xof_math_subst_itm itm = new Xof_math_subst_itm(src_bry, Bry_.new_a7(trg_str), dollarSign, wholeWord);
|
||||
trie.Add_obj(src_bry, itm);
|
||||
}
|
||||
private Btrie_slim_mgr trie = Btrie_slim_mgr.cs_();
|
||||
}
|
||||
class Xof_math_subst_itm {
|
||||
public int SrcLen() {return src_len;} private int src_len;
|
||||
public byte[] Src() {return src;} private byte[] src;
|
||||
public byte[] Trg() {return trg;} private byte[] trg;
|
||||
public boolean DollarSign() {return dollarSign;} private boolean dollarSign;
|
||||
public boolean WholeWord() {return wholeWord;} private boolean wholeWord;
|
||||
public Xof_math_subst_itm(byte[] src, byte[] trg, boolean dollarSign, boolean wholeWord) {this.src = src; src_len = src.length; this.trg = trg; this.dollarSign = dollarSign; this.wholeWord = wholeWord;}
|
||||
}
|
||||
Reference in New Issue
Block a user