Clean up custom javascript injection code; add helper method Add_tag

v3.3.4
gnosygnu 8 years ago
parent 48cb88f655
commit 7ccea7ac7a

@ -18,11 +18,16 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_doc {
private final Bry_bfr bfr;
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
public Xoscript_doc(Bry_bfr bfr, Xoscript_page page) {
this.bfr = bfr;
this.page = page;
this.head = new Xoscript_doc_head(this);
this.tail = new Xoscript_doc_tail(this);
head.Reg_marker("<!--XOWA.SCRIPT.HEAD.TOP-->", "top", Xoscript_doc_sect_base.Pos__default);
head.Reg_marker("<!--XOWA.SCRIPT.HEAD.BOT-->", "bot");
tail.Reg_marker("<!--XOWA.SCRIPT.TAIL.TOP-->", "top", Xoscript_doc_sect_base.Pos__default);
}
public Xoscript_page Page() {return page;} private final Xoscript_page page;
public Xoscript_doc_head Head() {return head;} private final Xoscript_doc_head head;
@ -37,6 +42,19 @@ public class Xoscript_doc {
html = s;
html_dirty = true;
}
public void Html_by_marker_(byte[] marker, byte[] marker_html) {
byte[] html_bry = Bry_.new_u8(this.Html());
// find marker, and splice it in
int marker_pos = Bry_find_.Find_fwd(html_bry, marker);
if (marker_pos != Bry_find_.Not_found) {
tmp_bfr.Add_mid(html_bry, 0, marker_pos);
tmp_bfr.Add(marker_html);
tmp_bfr.Add_mid(html_bry, marker_pos, html_bry.length);
html_bry = tmp_bfr.To_bry_and_clear();
}
Html_(String_.new_u8(html_bry));
}
public boolean Dirty() {
return html_dirty;
} private boolean html_dirty;

@ -16,17 +16,6 @@ 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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_doc_head {
private final Xoscript_doc doc;
public Xoscript_doc_head(Xoscript_doc doc) {this.doc = doc;}
public void Add_js_file(String file, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.HEAD.TOP-->" : "<!--XOWA.SCRIPT.HEAD.BOT-->";
if (String_.Has_at_bgn(file, "./")) file = String_.Replace(file, "./", doc.Page().Env().Root_dir().To_http_file_str());
String elem = "<script src=\"" + file + "\" type=\"text/javascript\"></script>\n";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
public void Add_elem(String elem, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.HEAD.TOP-->" : "<!--XOWA.SCRIPT.HEAD.BOT-->";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
public class Xoscript_doc_head extends Xoscript_doc_sect_base {
public Xoscript_doc_head(Xoscript_doc doc) {super(doc);}
}

@ -0,0 +1,74 @@
/*
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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
import org.junit.*; import gplx.core.tests.*;
public class Xoscript_doc_head__tst {
private final Xoscript_doc_head__fxt fxt = new Xoscript_doc_head__fxt();
@Before public void init() {
fxt.Init__sect("head");
fxt.Exec__reg_marker("<!--top-->", "top", Xoscript_doc_head.Pos__default);
fxt.Exec__reg_marker("<!--bot-->", "bot");
fxt.Exec__doc__html("a<!--top-->b<!--bot-->c");
}
@Test public void Add_html() {
fxt.Exec__add_html("top", "<b>add_1</b>");
fxt.Exec__add_html("top", "<b>add_2</b>");
fxt.Test__html("a<b>add_1</b><b>add_2</b><!--top-->b<!--bot-->c");
}
@Test public void Add_html__default() {
fxt.Exec__add_html("<b>add_1</b>");
fxt.Test__html("a<b>add_1</b><!--top-->b<!--bot-->c");
}
@Test public void Add_tag() {
fxt.Exec__add_tag("top", "div", "div_body", "k0", "v0", "k1", "v1");
fxt.Test__html
( "a<div k0=\"v0\" k1=\"v1\">div_body</div>"
, "<!--top-->b<!--bot-->c");
}
@Test public void Add_js_file() {
fxt.Exec__add_js_file("top", "./a.js");
fxt.Test__html
( "a<script src=\"file:///mem/wiki/test_wiki/bin/script/a.js\" type=\"text/javascript\"></script>"
, "<!--top-->b<!--bot-->c");
}
}
class Xoscript_doc_head__fxt {
private final Xoscript_page spg;
private Xoscript_doc_sect_base sect;
public Xoscript_doc_head__fxt() {
Bry_bfr rv = Bry_bfr_.New();
Xoscript_env env = new Xoscript_env(Io_url_.new_any_("mem/wiki/test_wiki/bin/script/"));
Xoscript_url url = new Xoscript_url("test_wiki", "test_page");
spg = new Xoscript_page(rv, env, url);
}
public void Init__sect(String sect_name) {
if (String_.Eq(sect_name, "head"))
sect = spg.Doc().Head();
else if (String_.Eq(sect_name, "tail"))
sect = spg.Doc().Tail();
}
public void Exec__doc__html(String html) {spg.Doc().Html_(html);}
public void Exec__reg_marker(String marker, String... pos_ary) {sect.Reg_marker(marker, pos_ary);}
public void Exec__add_js_file(String pos, String file) {sect.Add_js_file(pos, file);}
public void Exec__add_html(String html) {sect.Add_html(html);}
public void Exec__add_html(String pos, String html) {sect.Add_html(pos, html);}
public void Exec__add_tag(String pos, String tag, String body, String... head_atrs) {sect.Add_tag(pos, tag, body, head_atrs);}
public void Test__html(String... expd) {
Gftest.Eq__ary__lines(String_.Concat_lines_nl_skip_last(expd), spg.Doc().Html(), "html");
}
}

@ -0,0 +1,81 @@
/*
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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public abstract class Xoscript_doc_sect_base {
protected final Xoscript_doc doc;
private final Hash_adp_bry marker_hash = Hash_adp_bry.cs();
private final Bry_bfr tmp_bfr = Bry_bfr_.New();
public Xoscript_doc_sect_base(Xoscript_doc doc) {this.doc = doc;}
private byte[] Get_marker_by_pos(byte[] pos_bry) {
return (byte[])marker_hash.Get_by_or_fail(pos_bry);
}
public void Reg_marker(String marker_str, String... pos_ary) {
int len = pos_ary.length;
byte[] marker_bry = Bry_.new_u8(marker_str);
for (int i = 0; i < len; ++i) {
marker_hash.Add_if_dupe_use_nth(Bry_.new_u8(pos_ary[i]), marker_bry);
}
}
public void Add_html(String html) {Add_html(Pos__default, html);}
public void Add_html(String pos_str, String html) {Add_html(Pos__default, Bry_.new_u8(html));}
public void Add_html(String pos_str, byte[] html) {
doc.Html_by_marker_(Get_marker_by_pos(Bry_.new_u8(pos_str)), html);
}
public void Add_tag(String pos_str, String tag_str, String body, String... head_atrs) {
// build tag.bgn; EX: '<tag k1="v1">'
tmp_bfr.Add_byte(Byte_ascii.Angle_bgn);
tmp_bfr.Add_str_u8(tag_str);
int head_atrs_len = head_atrs.length;
for (int i = 0; i < head_atrs_len; i += 2) {
tmp_bfr.Add_byte_space();
tmp_bfr.Add_str_u8(head_atrs[i]);
tmp_bfr.Add_byte_eq();
tmp_bfr.Add_byte_quote();
tmp_bfr.Add_str_u8(head_atrs[i + 1]);
tmp_bfr.Add_byte_quote();
}
tmp_bfr.Add_byte(Byte_ascii.Angle_end);
// build tag.body; EX: 'some body'
tmp_bfr.Add_str_u8(body);
// build tag.end; EX: '</tag>\n'
tmp_bfr.Add_byte(Byte_ascii.Angle_bgn).Add_byte(Byte_ascii.Slash);
tmp_bfr.Add_str_u8(tag_str);
tmp_bfr.Add_byte(Byte_ascii.Angle_end);
tmp_bfr.Add_byte_nl();
Add_html(pos_str, tmp_bfr.To_bry_and_clear());
}
public void Add_js_file(String file_str) {Add_js_file(file_str, Pos__default);}
public void Add_js_file(String pos_str, String file_str) {
// resolve file
file_str = Resolve_file(file_str, doc.Page().Env().Root_dir());
// build script tag
Add_tag(pos_str, "script", Body__empty, "src", file_str, "type", "text/javascript");
}
private String Resolve_file(String file, Io_url root_dir) {
String rv = file;
// resolve relative urls; EX: "./a.js" -> "/xowa/wiki/simple.wikipedia.org/bin/script/a.js"
if (String_.Has_at_bgn(file, "./"))
rv = String_.Replace(file, "./", root_dir.To_http_file_str());
return rv;
}
public static final String Pos__default = "default", Body__empty = "";
}

@ -16,17 +16,6 @@ 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.addons.htmls.scripts.apis; import gplx.*; import gplx.xowa.*; import gplx.xowa.addons.*; import gplx.xowa.addons.htmls.*; import gplx.xowa.addons.htmls.scripts.*;
public class Xoscript_doc_tail {
private final Xoscript_doc doc;
public Xoscript_doc_tail(Xoscript_doc doc) {this.doc = doc;}
public void Add_js_file(String file, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.TAIL-->" : "<!--XOWA.SCRIPT.TAIL-->";
if (String_.Has_at_bgn(file, "./")) file = String_.Replace(file, "./", doc.Page().Env().Root_dir().To_http_file_str());
String elem = "<script src=\"" + file + "\" type=\"text/javascript\"></script>\n";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
public void Add_elem(String elem, String pos) {
String marker = String_.Eq(pos, "top") ? "<!--XOWA.SCRIPT.TAIL-->" : "<!--XOWA.SCRIPT.TAIL-->";
doc.Html_(String_.Replace(doc.Html(), marker, marker + elem));
}
public class Xoscript_doc_tail extends Xoscript_doc_sect_base {
public Xoscript_doc_tail(Xoscript_doc doc) {super(doc);}
}

@ -22,7 +22,7 @@ import gplx.core.brys.args.*; import gplx.core.threads.*; import gplx.xowa.bldrs
import gplx.xowa.wikis.domains.*;
import gplx.xowa.bldrs.wms.*; import gplx.xowa.bldrs.wms.dumps.*;
public class Xoi_cmd_wiki_tst {
@Test public void Run() { // MAINT
@Test public void Run() { // MAINT:2016-10-13
// Bld_import_list(Xow_domain_regy.All);
// Bld_cfg_files(Xow_domain_regy.All); // NOTE: remember to carry over the wikisource / page / index commands from the existing xowa_build_cfg.gfs; also, only run the xowa_build_cfg.gfs once; DATE:2013-10-15; last run: DATE:2014-09-09
}

@ -41,7 +41,7 @@ public class Wmf_dump_list_parser_tst {
, fxt.itm("zh-classicalwiki", "20131128", Wmf_dump_itm.Status_tid_complete, "Dump complete", "2013-11-28 06:08:56")
);
}
// @Test public void Update() { // MAINT:QUARTERLY:2016-07-03; must run C:\xowa\ and update dump status
// @Test public void Update() { // MAINT:QUARTERLY:2016-10-13; must run home/wiki/Dashboard/Wiki_maintenance and click "update dump status"
// Hash_adp_bry excluded_domains = Hash_adp_bry.cs().Add_many_str
// ( "advisory.wikipedia.org", "beta.wikiversity.org", "donate.wikipedia.org", "login.wikipedia.org"
// , "nostalgia.wikipedia.org", "outreach.wikipedia.org", "quality.wikipedia.org", "sources.wikipedia.org"

Loading…
Cancel
Save