mirror of https://github.com/gnosygnu/xowa
parent
48cb88f655
commit
7ccea7ac7a
@ -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 = "";
|
||||||
|
}
|
Loading…
Reference in new issue