From 7ccea7ac7af6bb65df209e09e6e9b83bf1eb0619 Mon Sep 17 00:00:00 2001 From: gnosygnu Date: Fri, 14 Oct 2016 10:53:55 -0400 Subject: [PATCH] Clean up custom javascript injection code; add helper method Add_tag --- .../htmls/scripts/apis/Xoscript_doc.java | 18 +++++ .../htmls/scripts/apis/Xoscript_doc_head.java | 15 +--- .../scripts/apis/Xoscript_doc_head__tst.java | 74 +++++++++++++++++ .../scripts/apis/Xoscript_doc_sect_base.java | 81 +++++++++++++++++++ .../htmls/scripts/apis/Xoscript_doc_tail.java | 15 +--- .../xowa/bldrs/installs/Xoi_cmd_wiki_tst.java | 2 +- .../maints/Wmf_dump_list_parser_tst.java | 2 +- 7 files changed, 179 insertions(+), 28 deletions(-) create mode 100644 400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head__tst.java create mode 100644 400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_sect_base.java diff --git a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc.java b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc.java index 4298aefc1..d1bcda9de 100644 --- a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc.java +++ b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc.java @@ -18,11 +18,16 @@ along with this program. If not, see . 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("", "top", Xoscript_doc_sect_base.Pos__default); + head.Reg_marker("", "bot"); + tail.Reg_marker("", "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; diff --git a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head.java b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head.java index 410387555..835eac57a 100644 --- a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head.java +++ b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head.java @@ -16,17 +16,6 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ 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") ? "" : ""; - if (String_.Has_at_bgn(file, "./")) file = String_.Replace(file, "./", doc.Page().Env().Root_dir().To_http_file_str()); - String elem = "\n"; - doc.Html_(String_.Replace(doc.Html(), marker, marker + elem)); - } - public void Add_elem(String elem, String pos) { - String marker = String_.Eq(pos, "top") ? "" : ""; - 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);} } diff --git a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head__tst.java b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head__tst.java new file mode 100644 index 000000000..de4091c4c --- /dev/null +++ b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_head__tst.java @@ -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 . +*/ +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", Xoscript_doc_head.Pos__default); + fxt.Exec__reg_marker("", "bot"); + fxt.Exec__doc__html("abc"); + } + @Test public void Add_html() { + fxt.Exec__add_html("top", "add_1"); + fxt.Exec__add_html("top", "add_2"); + fxt.Test__html("aadd_1add_2bc"); + } + @Test public void Add_html__default() { + fxt.Exec__add_html("add_1"); + fxt.Test__html("aadd_1bc"); + } + @Test public void Add_tag() { + fxt.Exec__add_tag("top", "div", "div_body", "k0", "v0", "k1", "v1"); + fxt.Test__html + ( "a
div_body
" + , "bc"); + } + @Test public void Add_js_file() { + fxt.Exec__add_js_file("top", "./a.js"); + fxt.Test__html + ( "a" + , "bc"); + } +} +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"); + } +} diff --git a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_sect_base.java b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_sect_base.java new file mode 100644 index 000000000..35ede3dda --- /dev/null +++ b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_sect_base.java @@ -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 . +*/ +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: '' + 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: '\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 = ""; +} diff --git a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_tail.java b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_tail.java index 04605d087..53824eba5 100644 --- a/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_tail.java +++ b/400_xowa/src/gplx/xowa/addons/htmls/scripts/apis/Xoscript_doc_tail.java @@ -16,17 +16,6 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ 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") ? "" : ""; - if (String_.Has_at_bgn(file, "./")) file = String_.Replace(file, "./", doc.Page().Env().Root_dir().To_http_file_str()); - String elem = "\n"; - doc.Html_(String_.Replace(doc.Html(), marker, marker + elem)); - } - public void Add_elem(String elem, String pos) { - String marker = String_.Eq(pos, "top") ? "" : ""; - 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);} } diff --git a/400_xowa/src/gplx/xowa/bldrs/installs/Xoi_cmd_wiki_tst.java b/400_xowa/src/gplx/xowa/bldrs/installs/Xoi_cmd_wiki_tst.java index 447704bae..75ea19beb 100644 --- a/400_xowa/src/gplx/xowa/bldrs/installs/Xoi_cmd_wiki_tst.java +++ b/400_xowa/src/gplx/xowa/bldrs/installs/Xoi_cmd_wiki_tst.java @@ -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 } diff --git a/400_xowa/src/gplx/xowa/bldrs/setups/maints/Wmf_dump_list_parser_tst.java b/400_xowa/src/gplx/xowa/bldrs/setups/maints/Wmf_dump_list_parser_tst.java index b34e99e77..25699ba0d 100644 --- a/400_xowa/src/gplx/xowa/bldrs/setups/maints/Wmf_dump_list_parser_tst.java +++ b/400_xowa/src/gplx/xowa/bldrs/setups/maints/Wmf_dump_list_parser_tst.java @@ -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"