mirror of
https://github.com/gnosygnu/xowa.git
synced 2024-10-27 20:34:16 +00:00
Math: Refactor Math classes
This commit is contained in:
parent
32fdbc6fbe
commit
62c81e6d77
@ -280,7 +280,8 @@ public class Gfh_tag_ { // NOTE: not serialized; used by tag_rdr
|
||||
, Comm_end_len = Comm_end.length
|
||||
;
|
||||
private static final byte[] Rhs_bgn = Bry_.new_a7("</");
|
||||
public static void Bld_lhs_end_nde(Bry_bfr bfr) {bfr.Add_byte(Byte_ascii.Gt);}
|
||||
public static void Bld_lhs_end_inl(Bry_bfr bfr) {bfr.Add_byte(Byte_ascii.Slash).Add_byte(Byte_ascii.Gt);}
|
||||
public static void Bld_rhs(Bry_bfr bfr, byte[] name) {bfr.Add(Rhs_bgn).Add(name).Add_byte(Byte_ascii.Angle_end);} // EX:"</tag_name>"
|
||||
public static void Bld_lhs_bgn(Bry_bfr bfr, byte[] tag) {bfr.Add_byte(Byte_ascii.Lt).Add(tag);} // >
|
||||
public static void Bld_lhs_end_nde(Bry_bfr bfr) {bfr.Add_byte(Byte_ascii.Gt);} // >
|
||||
public static void Bld_lhs_end_inl(Bry_bfr bfr) {bfr.Add_byte(Byte_ascii.Slash).Add_byte(Byte_ascii.Gt);} // "/>"
|
||||
public static void Bld_rhs(Bry_bfr bfr, byte[] name) {bfr.Add(Rhs_bgn).Add(name).Add_byte(Byte_ascii.Angle_end);} // EX:"</tag_name>"
|
||||
}
|
||||
|
@ -115,7 +115,6 @@ public class Xoae_app implements Xoa_app, Gfo_invk {
|
||||
public Xoa_shell Shell() {return shell;} private Xoa_shell shell;
|
||||
public Xoa_thread_mgr Thread_mgr_old() {return thread_mgr_old;} private Xoa_thread_mgr thread_mgr_old = new Xoa_thread_mgr();
|
||||
public Xoa_hive_mgr Hive_mgr() {return hive_mgr;} private Xoa_hive_mgr hive_mgr;
|
||||
public Xof_math_subst_regy Math_subst_regy() {return math_subst_regy;} private Xof_math_subst_regy math_subst_regy = new Xof_math_subst_regy();
|
||||
public Xoa_prog_mgr Prog_mgr() {return prog_mgr;} private final Xoa_prog_mgr prog_mgr = new Xoa_prog_mgr();
|
||||
public Gfo_async_mgr Async_mgr() {return async_mgr;} private Gfo_async_mgr async_mgr = new Gfo_async_mgr();
|
||||
|
||||
@ -160,7 +159,6 @@ public class Xoae_app implements Xoa_app, Gfo_invk {
|
||||
api_root.Init_by_app(this);
|
||||
wmf_mgr.Init_by_app(this);
|
||||
gplx.core.net.emails.Gfo_email_mgr_.Instance = gplx.core.net.emails.Gfo_email_mgr_.New_jre();
|
||||
file_mgr.Init_by_app(this);
|
||||
special_mgr.Init_by_app(this);
|
||||
sys_cfg.Init_by_app(this);
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ public class Xobldr__lnki_temp__create extends Xob_dump_mgr_base implements gplx
|
||||
gplx.xowa.parsers.xndes.Xop_xnde_wkr.Timeline_log_wkr = log_mgr.Make_wkr();
|
||||
gplx.xowa.xtns.scores.Score_xnde.Log_wkr = log_mgr.Make_wkr();
|
||||
gplx.xowa.xtns.hieros.Hiero_xnde.Log_wkr = log_mgr.Make_wkr();
|
||||
gplx.xowa.xtns.math.Math_nde.Log_wkr = log_mgr.Make_wkr().Save_src_str_(Bool_.Y); // enabled; DATE:2015-10-10
|
||||
gplx.xowa.xtns.math.Xomath_xnde.Log_wkr = log_mgr.Make_wkr().Save_src_str_(Bool_.Y); // enabled; DATE:2015-10-10
|
||||
|
||||
// init fsdb
|
||||
Xof_fsdb_mgr__sql trg_fsdb_mgr = new Xof_fsdb_mgr__sql();
|
||||
|
@ -59,8 +59,8 @@ public class Xoa_prog_mgr implements Gfo_invk {
|
||||
public Process_adp App_query_img_size() {return app_query_img_size;} private Process_adp app_query_img_size = new Process_adp();
|
||||
public Process_adp App_resize_img() {return app_resize_img;} private Process_adp app_resize_img = new Process_adp();
|
||||
public Process_adp App_convert_svg_to_png() {return app_convert_svg_to_png;} private Process_adp app_convert_svg_to_png = new Process_adp();
|
||||
public Process_adp App_convert_tex_to_dvi() {return app_convert_tex_to_dvi;} private Process_adp app_convert_tex_to_dvi = new Process_adp();
|
||||
public Process_adp App_convert_dvi_to_png() {return app_convert_dvi_to_png;} private Process_adp app_convert_dvi_to_png = new Process_adp();
|
||||
public Process_adp App__tex_to_dvi() {return app_convert_tex_to_dvi;} private Process_adp app_convert_tex_to_dvi = new Process_adp();
|
||||
public Process_adp App__dvi_to_png() {return app_convert_dvi_to_png;} private Process_adp app_convert_dvi_to_png = new Process_adp();
|
||||
public Process_adp App_convert_djvu_to_tiff() {return app_convert_djvu_to_tiff;} private Process_adp app_convert_djvu_to_tiff = new Process_adp();
|
||||
public Process_adp App_view_text() {return app_view_text;} private Process_adp app_view_text = new Process_adp();
|
||||
public Process_adp App_decompress_bz2() {return app_decompress_bz2;} private Process_adp app_decompress_bz2 = new Process_adp();
|
||||
|
@ -24,7 +24,6 @@ public class Xof_file_mgr implements Gfo_invk {
|
||||
public Xoa_repo_mgr Repo_mgr() {return repo_mgr;} private Xoa_repo_mgr repo_mgr;
|
||||
public Xof_img_mgr Img_mgr() {return img_mgr;} private final Xof_img_mgr img_mgr = new Xof_img_mgr();
|
||||
public Xof_cache_mgr Cache_mgr() {return cache_mgr;} private Xof_cache_mgr cache_mgr;
|
||||
public Xof_math_mgr Math_mgr() {return math_mgr;} private final Xof_math_mgr math_mgr = new Xof_math_mgr();
|
||||
public Xof_rule_mgr Ext_rules() {return ext_rules;} private final Xof_rule_mgr ext_rules = new Xof_rule_mgr();
|
||||
public void Ctor_by_app(Xoae_app app) {
|
||||
Gfo_usr_dlg usr_dlg = app.Usr_dlg();
|
||||
@ -32,15 +31,11 @@ public class Xof_file_mgr implements Gfo_invk {
|
||||
this.repo_mgr = new Xoa_repo_mgr(app.Fsys_mgr(), ext_rules);
|
||||
img_mgr.Init_by_app(app.Wmf_mgr(), app.Prog_mgr());
|
||||
}
|
||||
public void Init_by_app(Xoae_app app) {
|
||||
math_mgr.Init_by_app(app);
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Invk_repos)) return repo_mgr;
|
||||
else if (ctx.Match(k, Invk_img_mgr)) return img_mgr;
|
||||
else if (ctx.Match(k, Invk_ext_rules)) return ext_rules;
|
||||
else if (ctx.Match(k, Invk_math)) return math_mgr;
|
||||
else if (ctx.Match(k, Invk_cache_mgr)) return cache_mgr;
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
} private static final String Invk_repos = "repos", Invk_img_mgr= "img_mgr", Invk_ext_rules = "ext_rules", Invk_math = "math", Invk_cache_mgr = "cache_mgr";
|
||||
} private static final String Invk_repos = "repos", Invk_img_mgr= "img_mgr", Invk_ext_rules = "ext_rules", Invk_cache_mgr = "cache_mgr";
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ public class Xog_async_wkr {
|
||||
}
|
||||
|
||||
Async_imgs(usr_dlg, app, wiki, page, js_wkr);
|
||||
Async_math(usr_dlg, app, page, js_wkr);
|
||||
gplx.xowa.xtns.math.Xomath_latex_bldr.Async(app, page, js_wkr);
|
||||
Async_score(usr_dlg, app, page);
|
||||
Async_redlinks(usr_dlg, app, page, js_wkr);
|
||||
|
||||
@ -66,29 +66,6 @@ public class Xog_async_wkr {
|
||||
)
|
||||
js_wkr.Html_popups_bind_hover_to_doc(); // rebind all elements to popup
|
||||
}
|
||||
private static void Async_math(Gfo_usr_dlg usr_dlg, Xoae_app app, Xoae_page page, Xog_js_wkr js_wkr) {
|
||||
// make png from latex
|
||||
int len = page.File_math().Count();
|
||||
if (len == 0) return;
|
||||
try {
|
||||
usr_dlg.Prog_one("", "", "page.async.math; count=~{0}", len);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (usr_dlg.Canceled()) {
|
||||
usr_dlg.Prog_none("", "", "");
|
||||
app.Log_wtr().Queue_enabled_(false);
|
||||
return;
|
||||
}
|
||||
gplx.xowa.xtns.math.Xof_math_itm itm = (gplx.xowa.xtns.math.Xof_math_itm)page.File_math().Get_at(i);
|
||||
String queue_msg = usr_dlg.Prog_many("", "", "generating math ~{0} of ~{1}: ~{2}", i + List_adp_.Base1, len, itm.Math());
|
||||
app.File_mgr().Math_mgr().MakePng(itm.Math(), itm.Hash(), itm.Png_url(), queue_msg);
|
||||
gplx.gfui.SizeAdp size = app.File_mgr().Img_mgr().Wkr_query_img_size().Exec(itm.Png_url());
|
||||
js_wkr.Html_img_update("xowa_math_img_" + itm.Id(), itm.Png_url().To_http_file_str(), size.Width(), size.Height());
|
||||
js_wkr.Html_elem_delete("xowa_math_txt_" + itm.Id());
|
||||
}
|
||||
page.File_math().Clear();
|
||||
}
|
||||
catch (Exception e) {usr_dlg.Warn_many("", "", "page.async.math: page=~{0} err=~{1}", page.Ttl().Raw(), Err_.Message_gplx_log(e));}
|
||||
}
|
||||
private static void Async_score(Gfo_usr_dlg usr_dlg, Xoae_app app, Xoae_page page) {
|
||||
// run other cmds
|
||||
if (page.Html_cmd_mgr().Count() > 0) {
|
||||
|
@ -67,7 +67,7 @@ public class Xop_tkn_mkr {
|
||||
public gplx.xowa.xtns.poems.Poem_nde Xnde__poem() {return new gplx.xowa.xtns.poems.Poem_nde();}
|
||||
public Ref_nde Xnde__ref() {return new Ref_nde();}
|
||||
public References_nde Xnde__references() {return new References_nde();}
|
||||
public gplx.xowa.xtns.math.Math_nde Xnde__math() {return new gplx.xowa.xtns.math.Math_nde();}
|
||||
public gplx.xowa.xtns.math.Xomath_xnde Xnde__math() {return new gplx.xowa.xtns.math.Xomath_xnde();}
|
||||
public gplx.xowa.xtns.gallery.Gallery_xnde Xnde__gallery() {return new gplx.xowa.xtns.gallery.Gallery_xnde();}
|
||||
public gplx.xowa.xtns.imaps.Imap_xnde Xnde__imageMap() {return new gplx.xowa.xtns.imaps.Imap_xnde();}
|
||||
public gplx.xowa.xtns.hieros.Hiero_xnde Xnde__hiero() {return new gplx.xowa.xtns.hieros.Hiero_xnde();}
|
||||
|
@ -18,9 +18,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package gplx.xowa.parsers; import gplx.*; import gplx.xowa.*;
|
||||
import gplx.core.primitives.*; import gplx.core.btries.*;
|
||||
import gplx.xowa.files.*;
|
||||
import gplx.xowa.xtns.pfuncs.exprs.*; import gplx.xowa.xtns.math.*; import gplx.xowa.xtns.pfuncs.ttls.*;
|
||||
import gplx.xowa.xtns.pfuncs.exprs.*; import gplx.xowa.xtns.pfuncs.ttls.*;
|
||||
public class Xop_tmp_mgr {
|
||||
public Xof_math_itm Math_itm() {return math_itm;} private final Xof_math_itm math_itm = new Xof_math_itm();
|
||||
public Xof_xfer_itm Xfer_itm() {return xfer_itm;} private final Xof_xfer_itm xfer_itm = new Xof_xfer_itm();
|
||||
public Gfo_number_parser Pfunc_num_parser_0() {return num_parser_0;} private final Gfo_number_parser num_parser_0 = new Gfo_number_parser().Hex_enabled_(true);
|
||||
public Gfo_number_parser Pfunc_num_parser_1() {return num_parser_1;} private final Gfo_number_parser num_parser_1 = new Gfo_number_parser().Hex_enabled_(true);
|
||||
@ -28,8 +27,4 @@ public class Xop_tmp_mgr {
|
||||
public Btrie_slim_mgr Xnde__xtn_end() {return xnde__xtn_end;} private final Btrie_slim_mgr xnde__xtn_end = Btrie_slim_mgr.ci_a7(); // NOTE:ci.ascii:MW_const.en; listed XML node names are en
|
||||
public Btrie_rv Xnde__trv() {return xnde__trv;} private final Btrie_rv xnde__trv = new Btrie_rv();
|
||||
public Int_obj_ref Pfunc_rel2abs() {return pfunc_rel2abs;} private final Int_obj_ref pfunc_rel2abs = Int_obj_ref.New_zero();
|
||||
public Pfunc_anchorencode_mgr Pfunc_anchor_encoder() {
|
||||
// if (pf
|
||||
return null;
|
||||
} //private pfunc_anchor_encoder
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ import gplx.core.primitives.*; import gplx.core.brys.fmtrs.*;
|
||||
import gplx.xowa.wikis.*; import gplx.core.envs.*;
|
||||
import gplx.xowa.files.*;
|
||||
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.wbases.hwtrs.*; import gplx.xowa.xtns.pfuncs.ifs.*; import gplx.xowa.xtns.pfuncs.times.*; import gplx.xowa.xtns.pfuncs.ttls.*;
|
||||
import gplx.xowa.parsers.uniqs.*; import gplx.xowa.parsers.hdrs.sections.*;
|
||||
import gplx.xowa.xtns.math.*; import gplx.xowa.parsers.uniqs.*; import gplx.xowa.parsers.hdrs.sections.*;
|
||||
public class Xow_parser_mgr {
|
||||
private final Xowe_wiki wiki; private final Xop_tkn_mkr tkn_mkr;
|
||||
public Xow_parser_mgr(Xowe_wiki wiki) {
|
||||
@ -39,6 +39,7 @@ public class Xow_parser_mgr {
|
||||
public Gfo_number_parser Pp_num_parser() {return pp_num_parser;} private final Gfo_number_parser pp_num_parser = new Gfo_number_parser().Ignore_space_at_end_y_();
|
||||
public int[] Rel2abs_ary() {return rel2abs_ary;} private final int[] rel2abs_ary = new int[Pfunc_rel2abs.Ttl_max];
|
||||
public Xop_uniq_mgr Uniq_mgr() {return uniq_mgr;} private final Xop_uniq_mgr uniq_mgr = new Xop_uniq_mgr();
|
||||
public Xomath_core Math__core() {return math__core;} private final Xomath_core math__core = new Xomath_core();
|
||||
public boolean Lst__recursing() {return lst_recursing;} private boolean lst_recursing; public void Lst__recursing_(boolean v) {lst_recursing = v;}
|
||||
public Bry_bfr Wbase__time__bfr() {return wbase__time__bfr;} private final Bry_bfr wbase__time__bfr = Bry_bfr_.New();
|
||||
public Bry_fmtr Wbase__time__fmtr() {return wbase__time__fmtr;} private final Bry_fmtr wbase__time__fmtr = Bry_fmtr.new_();
|
||||
@ -75,6 +76,7 @@ public class Xow_parser_mgr {
|
||||
return rv;
|
||||
} private Pfunc_anchorencode_mgr anchor_encoder_mgr;
|
||||
public void Init_by_wiki() {
|
||||
math__core.Init_by_wiki(wiki);
|
||||
hdr__section_editable__mgr.Init_by_wiki(wiki);
|
||||
}
|
||||
public void Parse(Xoae_page page, boolean clear) { // main parse method; should never be called nested
|
||||
|
@ -1,98 +0,0 @@
|
||||
/*
|
||||
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.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) {
|
||||
Xoae_app app = ctx.App(); Xowe_wiki wiki = ctx.Wiki(); Xoae_page page = ctx.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 = wiki.Utl__bfr_mkr().Get_b512();
|
||||
try {
|
||||
math_bry = Escape_tex(tmp_bfr, !renderer_is_latex, math_bry);
|
||||
byte[] math_bry_clean = wiki.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();
|
||||
Xof_math_itm math_itm = ctx.Tmp_mgr().Math_itm();
|
||||
if (renderer_is_latex && app.File_mgr().Math_mgr().Find_itm(math_itm, page.Wiki().Domain_str(), math_bry)) {
|
||||
bfr.Add(Xoh_consts.Img_bgn);
|
||||
bfr.Add_str_u8(math_itm.Png_url().To_http_file_str());
|
||||
bfr.Add(Xoh_consts.__inline_quote);
|
||||
}
|
||||
else
|
||||
Write_for_mathjax(bfr, page, enabled, renderer_is_latex, math_bry, tmp_bfr, math_itm);
|
||||
} finally {tmp_bfr.Mkr_rls();}
|
||||
}
|
||||
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;
|
||||
math_fmtr.Bld_bfr_many(tmp_bfr, id, unique_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.langs.htmls.entitys.Gfh_entity_.Lt_bry; break;
|
||||
case Byte_ascii.Gt: if (mathjax) escaped = gplx.langs.htmls.entitys.Gfh_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.langs.htmls.Gfh_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.To_bry_and_clear() : bry;
|
||||
}
|
||||
}
|
@ -1,107 +0,0 @@
|
||||
/*
|
||||
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.brys.fmtrs.*; import gplx.core.strings.*; import gplx.core.consoles.*; import gplx.core.envs.*;
|
||||
import gplx.xowa.apps.progs.*;
|
||||
public class Xof_math_mgr implements Gfo_invk {
|
||||
private static final gplx.core.security.Hash_algo md5_hash = gplx.core.security.Hash_algo_.New__md5();
|
||||
private Xoae_app app;
|
||||
public Process_adp Cmd_convert_tex_to_dvi() {return cmd_convert_tex_to_dvi;} private Process_adp cmd_convert_tex_to_dvi = new Process_adp();
|
||||
public Process_adp Cmd_convert_dvi_to_png() {return cmd_convert_dvi_to_png;} private Process_adp cmd_convert_dvi_to_png = new Process_adp();
|
||||
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();
|
||||
app.Cfg().Bind_many_app(this, Cfg__enabled, Cfg__renderer);
|
||||
}
|
||||
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 = md5_hash.Hash_bry_as_str(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.Instance.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.To_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.Instance.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("", "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.Instance.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("", "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, Cfg__enabled)) enabled = m.ReadYn("v");
|
||||
else if (ctx.Match(k, Cfg__renderer)) renderer_is_mathjax = String_.Eq(m.ReadStr("v"), "mathjax");
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
}
|
||||
private static final String
|
||||
Cfg__enabled = "xowa.addon.math.enabled"
|
||||
, Cfg__renderer = "xowa.addon.math.renderer"
|
||||
;
|
||||
}
|
40
400_xowa/src/gplx/xowa/xtns/math/Xomath_core.java
Normal file
40
400_xowa/src/gplx/xowa/xtns/math/Xomath_core.java
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
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.parsers.*; import gplx.xowa.parsers.xndes.*;
|
||||
public class Xomath_core implements Gfo_invk {
|
||||
private final Xomath_html_wtr html_wtr = new Xomath_html_wtr();
|
||||
public boolean Enabled() {return enabled;} private boolean enabled = true;
|
||||
public boolean Renderer_is_mathjax() {return renderer_is_mathjax;} private boolean renderer_is_mathjax = true;
|
||||
public void Renderer_is_mathjax_(boolean v) {renderer_is_mathjax = v;} // TEST:
|
||||
public void Init_by_wiki(Xow_wiki wiki) {
|
||||
wiki.App().Cfg().Bind_many_wiki(this, wiki, Cfg__enabled, Cfg__renderer);
|
||||
}
|
||||
public void Write(Bry_bfr bfr, Xop_ctx ctx, Xop_xnde_tkn xnde, byte[] src) {
|
||||
html_wtr.Write(bfr, ctx, xnde, src, !renderer_is_mathjax, enabled);
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {
|
||||
if (ctx.Match(k, Cfg__enabled)) enabled = m.ReadYn("v");
|
||||
else if (ctx.Match(k, Cfg__renderer)) renderer_is_mathjax = String_.Eq(m.ReadStr("v"), "mathjax");
|
||||
else return Gfo_invk_.Rv_unhandled;
|
||||
return this;
|
||||
}
|
||||
private static final String
|
||||
Cfg__enabled = "xowa.addon.math.enabled"
|
||||
, Cfg__renderer = "xowa.addon.math.renderer";
|
||||
}
|
@ -17,36 +17,37 @@ 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() {
|
||||
fxt.App().File_mgr().Init_by_app(fxt.App());
|
||||
} private final Xop_fxt fxt = Xop_fxt.New_app_html();
|
||||
public class Xomath_core__tst {
|
||||
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);
|
||||
Renderer_(false);
|
||||
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);
|
||||
Renderer_(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);
|
||||
Renderer_(true);
|
||||
fxt.Test__parse_to_html_mgr("<math>a<>b</math>", "<span id='xowa_math_txt_0'>a<>b</span>");
|
||||
}
|
||||
@Test public void Escape__latex() {
|
||||
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(false);
|
||||
Renderer_(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.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
|
||||
Renderer_(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.App().File_mgr().Math_mgr().Renderer_is_mathjax_(true);
|
||||
Renderer_(true);
|
||||
fxt.Test__parse_to_html_mgr("<math>a\"b</math>", "<span id='xowa_math_txt_0'>a\"b</span>");
|
||||
}
|
||||
@Test public void Script() {
|
||||
fxt.App().File_mgr().Math_mgr().Renderer_is_mathjax_(false);
|
||||
Renderer_(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'><script>alert('fail');</script></span>");
|
||||
}
|
||||
private void Renderer_(boolean mathjax) {
|
||||
fxt.Wiki().Parser_mgr().Math__core().Renderer_is_mathjax_(mathjax);
|
||||
}
|
||||
}
|
105
400_xowa/src/gplx/xowa/xtns/math/Xomath_html_wtr.java
Normal file
105
400_xowa/src/gplx/xowa/xtns/math/Xomath_html_wtr.java
Normal file
@ -0,0 +1,105 @@
|
||||
/*
|
||||
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.envs.*;
|
||||
import gplx.langs.htmls.*; import gplx.langs.htmls.entitys.*; import gplx.xowa.htmls.core.htmls.*;
|
||||
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*;
|
||||
class Xomath_html_wtr {
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr_.New_w_size(512);
|
||||
private final Xomath_subst_mgr subst_mgr = new Xomath_subst_mgr();
|
||||
private final gplx.core.security.Hash_algo md5_wkr = gplx.core.security.Hash_algo_.New__md5();
|
||||
private final Bry_fmt
|
||||
fmt__latex = Bry_fmt.Auto( "<img id='xowa_math_img_~{math_idx}' src='' width='' height=''/><span id='xowa_math_txt_~{math_idx}'>~{math_text}</span>")
|
||||
, fmt__mathjax = Bry_fmt.Auto("<span id='xowa_math_txt_~{math_idx}'>~{math_text}</span>");
|
||||
|
||||
public void Write(Bry_bfr bfr, Xop_ctx ctx, Xop_xnde_tkn xnde, byte[] src, boolean is_latex, boolean enabled) {
|
||||
// init vars
|
||||
Xoae_app app = ctx.App(); Xowe_wiki wiki = ctx.Wiki(); Xoae_page page = ctx.Page();
|
||||
|
||||
// get math_bry; also, escape if mathjax and check js
|
||||
byte[] math_bry = Bry_.Mid(src, xnde.Tag_open_end(), xnde.Tag_close_bgn());
|
||||
if (!is_latex) math_bry = Escape_for_mathjax(tmp_bfr, math_bry);
|
||||
byte[] scrubbed_js = wiki.Html_mgr().Js_cleaner().Clean(wiki, math_bry, 0, math_bry.length);
|
||||
if (scrubbed_js != null) math_bry = scrubbed_js; // js found; use clean version; DATE:2013-08-26
|
||||
|
||||
// if latex, (a) calc md5 and url; (b) write <img> or add to queue
|
||||
int uid = page.File_math().Count();
|
||||
if (is_latex) {
|
||||
byte[] math_src = subst_mgr.Subst(math_bry);
|
||||
byte[] md5 = md5_wkr.Hash_bry_as_bry(math_src);
|
||||
|
||||
// make url
|
||||
byte dir_spr = Op_sys.Cur().Fsys_dir_spr_byte();
|
||||
tmp_bfr
|
||||
.Add_str_u8(app.Fsys_mgr().Root_dir().GenSubDir_nest("file", wiki.Domain_str(), "math").Raw()) // add dir
|
||||
.Add_byte(md5[0]).Add_byte(dir_spr)
|
||||
.Add_byte(md5[1]).Add_byte(dir_spr)
|
||||
.Add_byte(md5[2]).Add_byte(dir_spr)
|
||||
.Add(md5).Add_str_a7(".png");
|
||||
Io_url png = Io_url_.new_fil_(tmp_bfr.To_str_and_clear());
|
||||
|
||||
// if url exists, just write <img> directly
|
||||
if (Io_mgr.Instance.ExistsFil(png)) {
|
||||
Gfh_tag_.Bld_lhs_bgn(bfr, Gfh_tag_.Bry__img);
|
||||
Gfh_atr_.Add(bfr, Gfh_atr_.Bry__src, png.To_http_file_bry());
|
||||
Gfh_tag_.Bld_lhs_end_nde(bfr);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
// if enabled, add to pending queue; NOTE: must check enabled, else "downloading" prints at bottom of screen; also avoids file IO
|
||||
if (enabled)
|
||||
page.File_math().Add(new Xomath_latex_itm(uid, math_src, md5, png));
|
||||
}
|
||||
}
|
||||
|
||||
// write html: <span>math_expr</math>
|
||||
byte[] unique_bry = wiki.Parser_mgr().Uniq_mgr().Add(math_bry);
|
||||
Bry_fmt fmt = is_latex ? fmt__latex : fmt__mathjax;
|
||||
fmt.Bld_many(tmp_bfr, uid, unique_bry);
|
||||
bfr.Add_bfr_and_clear(tmp_bfr);
|
||||
}
|
||||
private static byte[] Escape_for_mathjax(Bry_bfr tmp, byte[] src) {
|
||||
if (src == null) return null;
|
||||
boolean dirty = false;
|
||||
byte[] escaped = null;
|
||||
|
||||
// loop bytes
|
||||
for (int i = 0; i < src.length; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Lt: escaped = Gfh_entity_.Lt_bry; break;
|
||||
case Byte_ascii.Gt: escaped = Gfh_entity_.Gt_bry; break;
|
||||
// case Byte_ascii.Amp: escaped = Const_amp; break; // TOMBSTONE:do not escape ampersand; PAGE:s.w:Matrix_(mathematics); DATE:2014-07-19
|
||||
// case Byte_ascii.Quote: escaped = Gfh_entity_.Quote_bry; break; // TOMBSTONE:do not escape quote; PAGE:s.w:Matrix_(mathematics); DATE:2014-07-19
|
||||
default:
|
||||
if (dirty)
|
||||
tmp.Add_byte(b);
|
||||
continue;
|
||||
}
|
||||
|
||||
// add escaped; note that only lt / gt will enter here; all other bytes are handled by "default / continue" above
|
||||
if (!dirty) {
|
||||
dirty = true;
|
||||
tmp.Add_mid(src, 0, i);
|
||||
}
|
||||
tmp.Add(escaped);
|
||||
escaped = null;
|
||||
}
|
||||
return dirty ? tmp.To_bry_and_clear() : src;
|
||||
}
|
||||
}
|
96
400_xowa/src/gplx/xowa/xtns/math/Xomath_latex_bldr.java
Normal file
96
400_xowa/src/gplx/xowa/xtns/math/Xomath_latex_bldr.java
Normal file
@ -0,0 +1,96 @@
|
||||
/*
|
||||
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.envs.*;
|
||||
public class Xomath_latex_bldr {
|
||||
public static void Async(Xoae_app app, Xoae_page page, gplx.xowa.guis.cbks.js.Xog_js_wkr js_wkr) {
|
||||
// get len; if 0, exit
|
||||
Gfo_usr_dlg usr_dlg = app.Usr_dlg();
|
||||
int len = page.File_math().Count();
|
||||
if (len == 0) return;
|
||||
|
||||
usr_dlg.Prog_one("", "", "page.async.math; count=~{0}", len);
|
||||
// loop each item
|
||||
for (int i = 0; i < len; ++i) {
|
||||
// if canceled, exit
|
||||
if (usr_dlg.Canceled()) {
|
||||
usr_dlg.Prog_none("", "", "");
|
||||
app.Log_wtr().Queue_enabled_(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// get itm and generate png
|
||||
Xomath_latex_itm itm = (Xomath_latex_itm)page.File_math().Get_at(i);
|
||||
String queue_msg = usr_dlg.Prog_many("", "", "generating math ~{0} of ~{1}: ~{2}", i + List_adp_.Base1, len, itm.Src());
|
||||
Generate_png(app, itm.Src(), itm.Md5(), itm.Png(), queue_msg);
|
||||
|
||||
// get png size; update <img> src; delete <span>
|
||||
gplx.gfui.SizeAdp size = app.File_mgr().Img_mgr().Wkr_query_img_size().Exec(itm.Png());
|
||||
js_wkr.Html_img_update("xowa_math_img_" + itm.Uid(), itm.Png().To_http_file_str(), size.Width(), size.Height());
|
||||
js_wkr.Html_elem_delete("xowa_math_txt_" + itm.Uid());
|
||||
}
|
||||
catch (Exception e) {
|
||||
usr_dlg.Warn_many("", "", "page.async.math: page=~{0} err=~{1}", page.Ttl().Raw(), Err_.Message_gplx_log(e));
|
||||
}
|
||||
}
|
||||
page.File_math().Clear();
|
||||
}
|
||||
private static void Generate_png(Xoae_app app, byte[] math, byte[] hash, Io_url png_url, String prog_fmt) {
|
||||
// init
|
||||
Io_url tmp_dir = app.Usere().Fsys_mgr().App_temp_dir().GenSubDir("math");
|
||||
Io_url tex_fil = tmp_dir.GenSubFil("xowa_math_temp.tex");
|
||||
|
||||
// make tex_bry
|
||||
byte[] tex_doc = Bry_.Replace(math, Bry_.new_a7("\n\n"), Byte_ascii.Nl_bry); // remove completely blank lines; not sure if this is right; PAGE:en.w:Standard Model_(mathematical_formulation); EX:<math>(\mathbf{1},\mathbf\n\n{1},0)</math>
|
||||
tex_doc = fmt__latex_doc.Bld_many_to_bry(Bry_bfr_.New(), tex_doc);
|
||||
Io_mgr.Instance.SaveFilBry(tex_fil, tex_doc);
|
||||
|
||||
// run tex to dvi
|
||||
Process_adp tex_to_dvi_cmd = app.Prog_mgr().App__tex_to_dvi();
|
||||
prog_fmt = String_.Replace(prog_fmt, "~", "~~"); // double-up ~ or else will break in progress bar
|
||||
tex_to_dvi_cmd.Prog_fmt_(prog_fmt + " tex_to_dvi: ~{process_seconds} second(s); ~{process_exe_name} ~{process_exe_args}");
|
||||
boolean pass = tex_to_dvi_cmd.Run(tex_fil.Raw(), tmp_dir.Xto_api()).Exit_code_pass();
|
||||
if (!pass)
|
||||
app.Usr_dlg().Warn_many("", "tex_to_dvi.fail", "fail: tex_to_dvi: error=~{0} latex=~{1}", tex_to_dvi_cmd.Rslt_out(), tex_doc);
|
||||
|
||||
// run dvi to png; NOTE: latex sometimes throws errors, but will generate .dvi; for sake of simplicity; always try to run dvipng
|
||||
Io_mgr.Instance.CreateDirIfAbsent(png_url.OwnerDir());
|
||||
Process_adp dvi_to_png_cmd = app.Prog_mgr().App__dvi_to_png();
|
||||
dvi_to_png_cmd.Prog_fmt_(prog_fmt + " dvi_to_png: ~{process_seconds} second(s); ~{process_exe_name} ~{process_exe_args}");
|
||||
pass = dvi_to_png_cmd.Run(tex_fil.GenNewExt(".dvi"), png_url, tmp_dir.Xto_api()).Exit_code_pass();
|
||||
if (!pass)
|
||||
app.Usr_dlg().Warn_many("", "dvi_to_png.fail", "fail: dvi_to_png: error=~{0} latex=~{1}", dvi_to_png_cmd.Rslt_out(), tex_doc);
|
||||
}
|
||||
private static final Bry_fmt fmt__latex_doc = Bry_fmt.Auto(Bry_.new_a7(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}"
|
||||
)));
|
||||
}
|
@ -16,13 +16,15 @@ 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);}
|
||||
class Xomath_latex_itm {
|
||||
public Xomath_latex_itm(int uid, byte[] src, byte[] md5, Io_url png) {
|
||||
this.uid = uid;
|
||||
this.src = src;
|
||||
this.md5 = md5;
|
||||
this.png = png;
|
||||
}
|
||||
public int Uid() {return uid;} private final int uid;
|
||||
public byte[] Src() {return src;} private final byte[] src;
|
||||
public byte[] Md5() {return md5;} private final byte[] md5;
|
||||
public Io_url Png() {return png;} private final Io_url png;
|
||||
}
|
@ -17,20 +17,26 @@ 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();
|
||||
class Xomath_subst_mgr {
|
||||
private final Btrie_slim_mgr trie = Btrie_slim_mgr.cs();
|
||||
private final Bry_bfr tmp = Bry_bfr_.New();
|
||||
private boolean init = false;
|
||||
public byte[] Subst(byte[] src) {
|
||||
if (!init) Init();
|
||||
int src_len = src.length;
|
||||
int dollarSignCount = 0;
|
||||
int inserted_dollars = 0;
|
||||
|
||||
// loop each byte
|
||||
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();
|
||||
if (o == null) // regular char; add to bfr
|
||||
tmp.Add_byte(b);
|
||||
else { // subst itm's trg for src
|
||||
Xomath_subst_itm itm = (Xomath_subst_itm)o;
|
||||
int itm_src_len = itm.Src_len();
|
||||
|
||||
// if whole_word, check last_char for exact match; exit if not
|
||||
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
|
||||
@ -41,24 +47,29 @@ public class Xof_math_subst_regy {
|
||||
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
|
||||
if (itm.Whole_word()) {
|
||||
tmp.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;
|
||||
|
||||
// add trg; increment dollar_sign; update i
|
||||
tmp.Add(itm.Trg());
|
||||
if (itm.Dollar_sign()) ++inserted_dollars; // if .Dollar_sign() is true, then tkn has inserted a dollar sign; will need to add matching closing item below
|
||||
i += itm_src_len - 1;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < dollarSignCount; i++)
|
||||
bfr.Add_byte(Byte_ascii.Dollar);
|
||||
return bfr.To_bry_and_clear_and_trim();
|
||||
} boolean init = false;
|
||||
public Xof_math_subst_regy Init() {
|
||||
|
||||
// add closing dollar-sign tokens for inserted_dollars
|
||||
for (int i = 0; i < inserted_dollars; i++)
|
||||
tmp.Add_byte(Byte_ascii.Dollar);
|
||||
|
||||
return tmp.To_bry_and_clear_and_trim();
|
||||
}
|
||||
private Xomath_subst_mgr Init() {
|
||||
if (init) return this;
|
||||
init = true;
|
||||
Reg("\\Alpha", "\\mathrm{A}");
|
||||
@ -159,18 +170,19 @@ public class Xof_math_subst_regy {
|
||||
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) {
|
||||
private void Reg(String src_str, String trg_str, boolean dollar_sign, boolean whole_word) {
|
||||
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);
|
||||
Xomath_subst_itm itm = new Xomath_subst_itm(src_bry, Bry_.new_a7(trg_str), dollar_sign, whole_word);
|
||||
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;}
|
||||
class Xomath_subst_itm {
|
||||
public Xomath_subst_itm(byte[] src, byte[] trg, boolean dollar_sign, boolean whole_word) {
|
||||
this.src = src; src_len = src.length; this.trg = trg; this.dollar_sign = dollar_sign; this.whole_word = whole_word;
|
||||
}
|
||||
public int Src_len() {return src_len;} private final int src_len;
|
||||
public byte[] Src() {return src;} private final byte[] src;
|
||||
public byte[] Trg() {return trg;} private final byte[] trg;
|
||||
public boolean Dollar_sign() {return dollar_sign;} private final boolean dollar_sign;
|
||||
public boolean Whole_word() {return whole_word;} private final boolean whole_word;
|
||||
}
|
@ -17,8 +17,8 @@ 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();
|
||||
public class Xomath_subst_mgr__tst {
|
||||
private final Xomath_subst_mgr subst_regy = new Xomath_subst_mgr();
|
||||
@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
|
@ -18,17 +18,17 @@ 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.htmls.*; import gplx.xowa.htmls.core.htmls.*;
|
||||
import gplx.xowa.parsers.*; import gplx.xowa.parsers.logs.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.htmls.*;
|
||||
public class Math_nde implements Xox_xnde {
|
||||
public class Xomath_xnde implements Xox_xnde {
|
||||
public Xop_xnde_tkn Xnde() {throw Err_.new_unimplemented();}
|
||||
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, Object xatr_id_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();
|
||||
Xomath_core math_mgr = wiki.Parser_mgr().Math__core();
|
||||
boolean log_wkr_enabled = Log_wkr != Xop_log_basic_wkr.Null; if (log_wkr_enabled) Log_wkr.Log_end_xnde(ctx.Page(), Xop_log_basic_wkr.Tid_math, src, xnde);
|
||||
if (math_mgr.Enabled() && math_mgr.Renderer_is_mathjax())
|
||||
ctx.Page().Html_data().Head_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, Xoae_page wpg, Xop_xnde_tkn xnde, byte[] src) {
|
||||
app.File_mgr().Math_mgr().Html_wtr().Write(html_wtr, ctx, hctx, bfr, src, xnde);
|
||||
ctx.Wiki().Parser_mgr().Math__core().Write(bfr, ctx, xnde, src);
|
||||
}
|
||||
public static Xop_log_basic_wkr Log_wkr = Xop_log_basic_wkr.Null;
|
||||
}
|
Loading…
Reference in New Issue
Block a user