1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

'v3.4.2.1'

This commit is contained in:
gnosygnu
2016-04-11 00:12:19 -04:00
parent b2a06b8125
commit 14471ca656
197 changed files with 1918 additions and 1275 deletions

View File

@@ -17,58 +17,95 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package gplx.xowa.xtns.wikias; 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.xndes.*; import gplx.xowa.parsers.htmls.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.htmls.*; import gplx.xowa.parsers.tmpls.*;
import gplx.core.btries.*;
public class Random_selection_xnde implements Xox_xnde {
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, Object xatr_id_obj) {}
import gplx.langs.htmls.*; import gplx.langs.htmls.docs.*;
public class Random_selection_xnde implements Xox_xnde, Mwh_atr_itm_owner2 {
private byte[] val = Bry_.Empty;
private byte[] choicetemplate = null, atr_before = null, atr_after = null;
private Rndsel_option_itm[] options_ary;
private int weight_total = 0;
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, byte xatr_id) {
switch (xatr_id) {
case Xatr__before: atr_before = xatr.Val_as_bry(); break;
case Xatr__after: atr_after = xatr.Val_as_bry(); break;
}
}
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
ctx.Para().Process_block__xnde(xnde.Tag(), Xop_xnde_tag.Block_bgn);
// Btrie_slim_mgr sub_xnde_trie = Btrie_slim_mgr.cs(); sub_xnde_trie.Add_obj("<option", 1).Add_obj("<choicetemplate", 2);
// int src_bgn = xnde.Src_bgn(); int src_end = xnde.Src_end();
// for (int i = src_bgn; i < src_end; ++i) {
// Object sub_xnde = sub_xnde_trie.Match_bgn(src, i, src_end); if (sub_xnde == null) continue;
// int pos = sub_xnde_trie.Match_pos();
// switch (src[pos]) {
// case Byte_ascii.Angle_end:
// break;
// case Byte_ascii.Space: case Byte_ascii.Tab: case Byte_ascii.Nl: case Byte_ascii.Cr:
// // find sub_xnde.Atrs()
// break;
// }
// int end_pos = Bry_find_.Find_fwd(src, Byte_ascii.Angle_bgn); // search for close tab
// i = end_pos;
// }
/*
int weight_total = 0;
int[] weight_nodes = new int[options_len];
for (int i = 0; i < options_len; ++i) {
Xnde x = get_option(i);
int weight_itm = x.Atr_int_or("weight", 1);
weight_total += 1;
}
if (weight_total == 0) return; // ignore empty <choose>
int rnd = RandomAdp_.new_().Next(weight_total);
byte[] option_bry = null;
for (int i = 0; i < options_len; ++i) {
r -= weight_nodes[i];
if (r <= 0) {
Xnde x = get_option(i);
option_bry = x.Text;
Xox_xnde_.Parse_xatrs(wiki, this, xatrs_hash, src, xnde);
// parse <choose>
List_adp option_list = List_adp_.new_();
Gfh_tag_rdr tag_rdr = Xox_xnde_.New_tag_rdr(ctx, src, xnde).Reg("option", Tag__option).Reg("choicetemplate", Tag__choicetemplate);
while (true) {
Gfh_tag head_tag = tag_rdr.Tag__move_fwd_head(Gfh_tag_.Id__any);
int tag_tid = head_tag.Name_id();
boolean eos = false;
switch (tag_tid) {
case Gfh_tag_.Id__eos: eos = true; break;
default: continue;
case Tag__option:
case Tag__choicetemplate: break;
}
if (eos) break;
int head_tag_end = head_tag.Src_end();
Gfh_tag tail_tag = tag_rdr.Tag__move_fwd_tail(tag_tid);
int tail_tag_bgn = tail_tag.Src_bgn();
switch (tag_tid) {
case Tag__choicetemplate: {
if (choicetemplate == null) { // only one <choicetemplate> should be specified; if many, always take 1st
choicetemplate = Bry_.Mid(src, head_tag_end, tail_tag_bgn);
}
break;
}
case Tag__option: {
int weight = tail_tag.Atrs__get_as_int_or(Atr__weight, 1);
option_list.Add(new Rndsel_option_itm(weight, Bry_.Mid(src, head_tag_end, tail_tag_bgn)));
weight_total += weight;
break;
}
}
}
if (templates_len > 0) {
Xnde x = get_option(0);
option_bry = "{{" + x.Text + "|" + option_bry + "}}";
if (weight_total == 0) return; // empty <choose> will be 0
this.options_ary = (Rndsel_option_itm[])option_list.To_ary_and_clear(Rndsel_option_itm.class);
// randomly select option_bry from all <option>s
byte[] option_bry = null;
int rnd = Rnd_test == -1 ? RandomAdp_.new_().Next(weight_total) + 1 : Rnd_test;
int options_len = options_ary.length;
for (int i = 0; i < options_len; ++i) {
Rndsel_option_itm option = options_ary[i];
rnd -= option.Weight();
if (rnd <= 0) {
option_bry = option.Text();
break;
}
}
if (atrs(before) != null)
atrs(before) = before + option_bry;
if (atrs(after) != null)
atrs(after) = option_bry + after;
this.val = ctx.Parse(option_bry)
*/
// decorate option_bry
if (choicetemplate != null) {
option_bry = Bry_.Add(Xop_curly_bgn_lxr.Hook, choicetemplate, Byte_ascii.Pipe_bry, option_bry, Xop_curly_end_lxr.Hook);
}
if (!Bry_.Eq(atr_before, Bry_.Empty)) option_bry = Bry_.Add(atr_before, option_bry);
if (!Bry_.Eq(atr_after , Bry_.Empty)) option_bry = Bry_.Add(option_bry, atr_after);
this.val = Xop_parser_.Parse_text_to_html(wiki, ctx.Page(), ctx.Page().Ttl(), option_bry, false);
ctx.Para().Process_block__xnde(xnde.Tag(), Xop_xnde_tag.Block_end);
}
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) {
// bfr.Add(val);
if (val != null) bfr.Add(val);
}
public static final byte Xatr__before = 0, Xatr__after = 1;
private static final Hash_adp_bry xatrs_hash = Hash_adp_bry.ci_a7().Add_str_byte("before", Xatr__before).Add_str_byte("after", Xatr__after);
private static final byte Tag__option = 0, Tag__choicetemplate = 1;
private static final byte[] Atr__weight = Bry_.new_a7("weight");
public static int Rnd_test = -1;
}
class Rndsel_option_itm {
public Rndsel_option_itm(int weight, byte[] text) {this.weight = weight; this.text = text;}
public int Weight() {return weight;} private final int weight;
public byte[] Text() {return text;} private final byte[] text;
}

View File

@@ -18,15 +18,30 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package gplx.xowa.xtns.wikias; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Random_selection_xnde_tst {
private final Xop_fxt fxt = new Xop_fxt();
private final Xop_fxt fxt = new Xop_fxt();
@Before public void init() {fxt.Reset();}
@Test public void Basic() {
Random_selection_xnde.Rnd_test = 2;
fxt.Test__parse__tmpl_to_html(String_.Concat_lines_nl_skip_last
( "<choose before=\"bgn_''\" after=\"''_end\">"
, "<option>A</option>"
, "<option>B</option>"
, "<option>C</option>"
, "</choose>"
), "bgn_<i>B</i>_end");
Random_selection_xnde.Rnd_test = -1;
}
@Test public void Choicetemplate() {
Random_selection_xnde.Rnd_test = 2;
fxt.Init_page_create("Template:Tmpl", "bgn_''{{{1}}}''_end");
fxt.Test__parse__tmpl_to_html(String_.Concat_lines_nl_skip_last
( "<choose>"
, "<option>a</option>"
, "<option>b</option>"
, "<option>c</option>"
, "<option>A</option>"
, "<option>B</option>"
, "<option>C</option>"
, "<choicetemplate>Tmpl</choicetemplate>"
, "</choose>"
), "");
), "bgn_<i>B</i>_end");
Random_selection_xnde.Rnd_test = -1;
}
}

View File

@@ -16,16 +16,46 @@ 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.wikias; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.xowa.htmls.*; import gplx.xowa.htmls.core.htmls.*;
import gplx.xowa.htmls.*; import gplx.langs.htmls.*; import gplx.xowa.htmls.core.htmls.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.htmls.*;
public class Tabber_xnde implements Xox_xnde {
private static final byte[] Spr__tab_itms = Bry_.new_a7("|-|");
private byte[] html;
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) {
ctx.Para().Process_block__xnde(xnde.Tag(), Xop_xnde_tag.Block_bgn);
ctx.Page().Html_data().Head_mgr().Itm__graph().Enabled_y_();
Bry_bfr tmp_bfr = Bry_bfr.new_();
tmp_bfr.Add_str_a7("<div id=\"tabber-\" "); // TODO: add key which is md5 of inner src
tmp_bfr.Add_str_a7("class=\"tabber\">\n");
// split on "|-|"; EX: "A|-|B" -> tab_1='A'; tab_2='B'
byte[][] tab_itms = Bry_split_.Split(src, xnde.Tag_open_end(), xnde.Tag_close_bgn(), Spr__tab_itms);
for (int i = 0; i < tab_itms.length; ++i) {
byte[] tab_itm = tab_itms[i];
tab_itm = Bry_.Trim(tab_itm);
int tab_itm_len = tab_itm.length; if (tab_itm_len == 0) continue;
// split on "="; EX: A=B -> tab_name='A'; tab_body = 'B'
byte[][] tab_parts = Bry_split_.Split(tab_itm, Byte_ascii.Eq);
byte[] tab_head = tab_parts[0];
byte[] tab_body = tab_parts.length == 1 ? Bry_.Empty : Xop_parser_.Parse_text_to_html(wiki, ctx.Page(), ctx.Page().Ttl(), tab_parts[1], false);
tmp_bfr.Add_str_a7("<div class=\"tabbertab\" title=\"");
tmp_bfr.Add(Gfh_utl.Escape_html_as_bry(tab_head));
tmp_bfr.Add_str_a7("\">\n");
tmp_bfr.Add(Gfh_tag_.P_lhs);
tmp_bfr.Add(tab_body);
tmp_bfr.Add(Gfh_tag_.P_rhs).Add_byte_nl();
tmp_bfr.Add(Gfh_tag_.Div_rhs).Add_byte_nl();
}
tmp_bfr.Add(Gfh_tag_.Div_rhs);
html = tmp_bfr.To_bry_and_clear();
ctx.Page().Html_data().Head_mgr().Itm__tabber().Enabled_y_();
ctx.Para().Process_block__xnde(xnde.Tag(), Xop_xnde_tag.Block_end);
}
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) {
bfr.Add_mid(src, xnde.Tag_open_end(), xnde.Tag_close_bgn());
if (html != null) bfr.Add(html);
}
}

View File

@@ -0,0 +1,54 @@
/*
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.wikias; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Tabber_xnde_tst {
private final Xop_fxt fxt = new Xop_fxt();
@Before public void init() {fxt.Reset();}
@Test public void Basic() {
fxt.Test__parse__tmpl_to_html(String_.Concat_lines_nl_skip_last
( "<tabber>"
, "tab1=''a''"
, "|-|"
, "tab2=''b''"
, "</tabber>"
), String_.Concat_lines_nl_skip_last
( "<div id=\"tabber-\" class=\"tabber\">"
, "<div class=\"tabbertab\" title=\"tab1\">"
, "<p><i>a</i></p>"
, "</div>"
, "<div class=\"tabbertab\" title=\"tab2\">"
, "<p><i>b</i></p>"
, "</div>"
, "</div>"
));
}
@Test public void Eq_missing() {
fxt.Test__parse__tmpl_to_html(String_.Concat_lines_nl_skip_last
( "<tabber>"
, "tab1"
, "</tabber>"
), String_.Concat_lines_nl_skip_last
( "<div id=\"tabber-\" class=\"tabber\">"
, "<div class=\"tabbertab\" title=\"tab1\">"
, "<p></p>"
, "</div>"
, "</div>"
));
}
}