mirror of https://github.com/gnosygnu/xowa
parent
686d56fdab
commit
52c36aa4f8
@ -0,0 +1,31 @@
|
||||
/*
|
||||
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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
public class Mustache_doc_itm_ {
|
||||
public static final byte[] Null_val = null;
|
||||
public static final Mustache_doc_itm Null_itm = null;
|
||||
public static final Mustache_doc_itm Itm__bool__y = new Mustache_doc_itm__bool(Bool_.Y), Itm__bool__n = new Mustache_doc_itm__bool(Bool_.N);
|
||||
public static final Mustache_doc_itm[] Ary__empty = new Mustache_doc_itm[0], Ary__bool__y = new Mustache_doc_itm[] {Itm__bool__y}, Ary__bool__n = new Mustache_doc_itm[] {Itm__bool__n};
|
||||
public static Mustache_doc_itm[] Ary__bool(boolean v) {return v ? Ary__bool__y : Ary__bool__n;}
|
||||
}
|
||||
class Mustache_doc_itm__bool implements Mustache_doc_itm {
|
||||
public Mustache_doc_itm__bool(boolean val) {this.val = val;}
|
||||
public boolean Val() {return val;} private final boolean val;
|
||||
public byte[] Get_prop(String key) {return Mustache_doc_itm_.Null_val;}
|
||||
public Mustache_doc_itm[] Get_subs(String key) {return Mustache_doc_itm_.Ary__empty;}
|
||||
}
|
@ -1,88 +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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
interface Mustache_elem_itm {
|
||||
int Tid();
|
||||
byte[] Key();
|
||||
Mustache_elem_itm[] Subs_ary();
|
||||
void Subs_ary_(Mustache_elem_itm[] v);
|
||||
void Render(Bry_bfr bfr, Mustache_render_ctx ctx);
|
||||
}
|
||||
class Mustache_elem_itm_ {// for types, see http://mustache.github.io/mustache.5.html
|
||||
public static final int Tid__root = 0, Tid__text = 1, Tid__variable = 2, Tid__escape = 3, Tid__section = 4, Tid__inverted = 5, Tid__comment = 6, Tid__partial = 7, Tid__delimiter = 8;
|
||||
public static final Mustache_elem_itm[] Ary_empty = new Mustache_elem_itm[0];
|
||||
}
|
||||
abstract class Mustache_elem_base implements Mustache_elem_itm {
|
||||
public Mustache_elem_base(int tid, byte[] key) {this.tid = tid; this.key = key;}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public byte[] Key() {return key;} private final byte[] key;
|
||||
@gplx.Virtual public Mustache_elem_itm[] Subs_ary() {return Mustache_elem_itm_.Ary_empty;}
|
||||
@gplx.Virtual public void Subs_ary_(Mustache_elem_itm[] v) {}
|
||||
@gplx.Virtual public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {}
|
||||
}
|
||||
class Mustache_elem_root extends Mustache_elem_base { // EX: {{variable}} -> <a>
|
||||
private Mustache_elem_itm[] subs_ary;
|
||||
public Mustache_elem_root() {super(Mustache_elem_itm_.Tid__root, Bry_.Empty);}
|
||||
@Override public Mustache_elem_itm[] Subs_ary() {return subs_ary;}
|
||||
@Override public void Subs_ary_(Mustache_elem_itm[] v) {subs_ary = v;}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
int subs_len = subs_ary.length;
|
||||
for (int i = 0; i < subs_len; ++i) {
|
||||
Mustache_elem_itm sub = subs_ary[i];
|
||||
sub.Render(bfr, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
class Mustache_elem_text extends Mustache_elem_base { // EX: text -> text
|
||||
private final byte[] src; private final int src_bgn, src_end;
|
||||
public Mustache_elem_text(byte[] src, int src_bgn, int src_end) {super(Mustache_elem_itm_.Tid__text, Bry_.Empty);
|
||||
this.src = src;
|
||||
this.src_bgn = src_bgn;
|
||||
this.src_end = src_end;
|
||||
}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
bfr.Add_mid(src, src_bgn, src_end);
|
||||
}
|
||||
}
|
||||
class Mustache_elem_variable extends Mustache_elem_base { // EX: {{variable}} -> <a>
|
||||
public Mustache_elem_variable(byte[] key) {super(Mustache_elem_itm_.Tid__variable, key);}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
byte[] key = this.Key();
|
||||
byte[] val = ctx.Render_variable(key);
|
||||
if (val != Mustache_doc_itm_.Null_val) // if not found, return empty String by default
|
||||
bfr.Add(val);
|
||||
}
|
||||
}
|
||||
class Mustache_elem_escape extends Mustache_elem_base { // EX: {{{variable}}} -> <a>
|
||||
public Mustache_elem_escape(byte[] key) {super(Mustache_elem_itm_.Tid__escape, key);}
|
||||
}
|
||||
class Mustache_elem_section extends Mustache_elem_base { // EX: {{#section}}val{{/section}} -> val (if boolean) or valvalval (if list)
|
||||
public Mustache_elem_section(byte[] key) {super(Mustache_elem_itm_.Tid__section, key);}
|
||||
}
|
||||
class Mustache_elem_inverted extends Mustache_elem_base { // EX: {{^section}}missing{{/section}} -> missing
|
||||
public Mustache_elem_inverted(byte[] key) {super(Mustache_elem_itm_.Tid__inverted, key);}
|
||||
}
|
||||
class Mustache_elem_comment extends Mustache_elem_base { // EX: {{!section}}commentent{{/section}} ->
|
||||
public Mustache_elem_comment(byte[] key) {super(Mustache_elem_itm_.Tid__inverted, key);}
|
||||
}
|
||||
class Mustache_elem_partial extends Mustache_elem_base { // EX: {{>a}} -> abc (deferred eval)
|
||||
public Mustache_elem_partial(byte[] key) {super(Mustache_elem_itm_.Tid__partial, key);}
|
||||
}
|
||||
class Mustache_elem_delimiter extends Mustache_elem_base {// EX: {{=<% %>=}} -> <% variable %>
|
||||
public Mustache_elem_delimiter(byte[] key) {super(Mustache_elem_itm_.Tid__delimiter, key);}
|
||||
}
|
@ -1,110 +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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
import gplx.core.btries.*;
|
||||
class Mustache_itm_parser {
|
||||
private byte[] src; private int src_end;
|
||||
private final Mustache_tkn_def tkn_def = new Mustache_tkn_def();
|
||||
public Mustache_elem_itm Parse(byte[] src, int src_bgn, int src_end) {
|
||||
this.src = src; this.src_end = src_end;
|
||||
Mustache_elem_root root = new Mustache_elem_root();
|
||||
Parse_grp(root, src_bgn);
|
||||
return root;
|
||||
}
|
||||
private void Parse_grp(Mustache_elem_itm owner, int src_bgn) {
|
||||
List_adp subs_list = List_adp_.new_();
|
||||
int pos = src_bgn;
|
||||
boolean loop = true;
|
||||
while (loop) {
|
||||
int tkn_lhs_bgn = Bry_find_.Find_fwd(src, tkn_def.Variable_lhs, pos, src_end); // next "{{"
|
||||
if (tkn_lhs_bgn == Bry_find_.Not_found) { // no "{{"; EOS
|
||||
loop = false;
|
||||
tkn_lhs_bgn = src_end;
|
||||
}
|
||||
subs_list.Add(new Mustache_elem_text(src, pos, tkn_lhs_bgn)); // add everything between last "}}" and cur "{{"
|
||||
if (!loop) break;
|
||||
pos = Parse_itm(subs_list, tkn_lhs_bgn + tkn_def.Variable_lhs_len);
|
||||
}
|
||||
if (subs_list.Count() > 0)
|
||||
owner.Subs_ary_((Mustache_elem_itm[])subs_list.To_ary_and_clear(Mustache_elem_itm.class));
|
||||
}
|
||||
private int Parse_itm(List_adp subs_list, int tkn_lhs_end) {
|
||||
if (tkn_lhs_end >= src_end) throw Fail(tkn_lhs_end, "early eos");
|
||||
byte b = src[tkn_lhs_end];
|
||||
int tkn_rhs_bgn = Bry_find_.Find_fwd(src, tkn_def.Variable_rhs, tkn_lhs_end, src_end);
|
||||
if (tkn_rhs_bgn == Bry_find_.Not_found) throw Fail(tkn_lhs_end, "dangling tkn");
|
||||
byte[] tkn_val = Bry_.Mid(src, tkn_lhs_end, tkn_rhs_bgn);
|
||||
Mustache_elem_itm elem = null;
|
||||
byte rhs_chk_byte = Byte_ascii.Null;
|
||||
switch (b) {
|
||||
default: elem = new Mustache_elem_variable(tkn_val); break;
|
||||
case Mustache_tkn_def.Comment: elem = new Mustache_elem_comment(tkn_val); break;
|
||||
case Mustache_tkn_def.Partial: elem = new Mustache_elem_partial(tkn_val); break;
|
||||
case Mustache_tkn_def.Delimiter_bgn: elem = new Mustache_elem_delimiter(tkn_val); rhs_chk_byte = Mustache_tkn_def.Delimiter_end; break; // TODO: change tkn_def{{=<% %>=}}
|
||||
case Mustache_tkn_def.Escape_bgn: elem = new Mustache_elem_escape(tkn_val); rhs_chk_byte = Mustache_tkn_def.Escape_end; break;
|
||||
case Mustache_tkn_def.Section: elem = new Mustache_elem_section(tkn_val); break;
|
||||
case Mustache_tkn_def.Inverted: elem = new Mustache_elem_inverted(tkn_val); break;
|
||||
case Mustache_tkn_def.Grp_end: break;
|
||||
}
|
||||
subs_list.Add(elem);
|
||||
if (rhs_chk_byte != Byte_ascii.Null) {
|
||||
if (src[tkn_rhs_bgn] != rhs_chk_byte) throw Fail(tkn_lhs_end, "invalid check byte");
|
||||
++tkn_rhs_bgn;
|
||||
}
|
||||
return tkn_rhs_bgn + tkn_def.Variable_rhs_len;
|
||||
}
|
||||
private Err Fail(int pos, String fmt, Object... args) {
|
||||
return Err_.new_("mustache", fmt, "excerpt", Bry_.Mid_by_len_safe(src, pos, 32));
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_def {
|
||||
public byte[] Variable_lhs = Dflt_variable_lhs;
|
||||
public byte[] Variable_rhs = Dflt_variable_rhs;
|
||||
public int Variable_lhs_len;
|
||||
public int Variable_rhs_len;
|
||||
public static final byte[]
|
||||
Dflt_variable_lhs = Bry_.new_a7("{{")
|
||||
, Dflt_variable_rhs = Bry_.new_a7("}}")
|
||||
;
|
||||
public static final byte
|
||||
Escape_bgn = Byte_ascii.Curly_bgn // {{{escape}}}
|
||||
, Escape_end = Byte_ascii.Curly_end // {{{escape}}}
|
||||
, Section = Byte_ascii.Hash // {{#section}}
|
||||
, Grp_end = Byte_ascii.Slash // {{/section}}
|
||||
, Inverted = Byte_ascii.Pow // {{^inverted}}
|
||||
, Comment = Byte_ascii.Bang // {{!comment}}
|
||||
, Partial = Byte_ascii.Angle_bgn // {{>partial}}
|
||||
, Delimiter_bgn = Byte_ascii.Eq // {{=<% %>=}}
|
||||
, Delimiter_end = Byte_ascii.Curly_end // {{=<% %>=}}
|
||||
;
|
||||
public Mustache_tkn_def() {
|
||||
Variable_lhs_len = Variable_lhs.length;
|
||||
Variable_rhs_len = Variable_rhs.length;
|
||||
}
|
||||
}
|
||||
/*
|
||||
root
|
||||
txt
|
||||
key
|
||||
txt
|
||||
section
|
||||
txt
|
||||
key
|
||||
txt
|
||||
txt
|
||||
*/
|
@ -0,0 +1,133 @@
|
||||
/*
|
||||
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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
import org.junit.*; import gplx.core.primitives.*;
|
||||
public class Mustache_itm_render_tst {
|
||||
private final Mustache_itm_render_fxt fxt = new Mustache_itm_render_fxt();
|
||||
@Test public void Text() {
|
||||
fxt.Test__parse("a b c", "a b c");
|
||||
}
|
||||
@Test public void Variable() {
|
||||
fxt.Init__root(fxt.Make_mock(0).Add_prop("prop1", "1").Add_prop("prop2", "2"));
|
||||
fxt.Test__parse("{{prop1}}", "1");
|
||||
fxt.Test__parse("a{{prop1}}b{{prop2}}c", "a1b2c");
|
||||
}
|
||||
@Test public void Escape() {
|
||||
fxt.Init__root(fxt.Make_mock(0).Add_prop("prop1", "<"));
|
||||
fxt.Test__parse("{{{prop1}}}", "<");
|
||||
fxt.Test__parse("{{prop1}}", "<");
|
||||
}
|
||||
@Test public void Section_bool() {
|
||||
fxt.Init__root(fxt.Make_mock(0).Add_bool_y("bool_y").Add_bool_n("bool_n"));
|
||||
fxt.Test__parse("a{{#bool_y}}b{{/bool_y}}c", "abc");
|
||||
fxt.Test__parse("a{{#bool_n}}b{{/bool_n}}c", "ac");
|
||||
fxt.Test__parse("a{{#bool_y}}b{{/bool_y}}c{{#bool_n}}d{{/bool_n}}e", "abce");
|
||||
}
|
||||
@Test public void Section_ws() {
|
||||
fxt.Init__root(fxt.Make_mock(0).Add_bool_y("bool_y"));
|
||||
fxt.Test__parse("a\n {{#bool_y}} \nb\n {{/bool_y}} \nc", "a\nb\nc");
|
||||
}
|
||||
@Test public void Section_subs_flat() {
|
||||
fxt.Init__root(fxt.Make_mock(0).Add_subs("subs1"
|
||||
, fxt.Make_mock(1).Add_prop("prop1", "1").Add_subs("subs2")
|
||||
, fxt.Make_mock(2).Add_prop("prop1", "2").Add_subs("subs2")
|
||||
));
|
||||
fxt.Test__parse("a{{#subs1}}({{prop1}}){{/subs1}}d", "a(1)(2)d");
|
||||
}
|
||||
@Test public void Section_subs_nest_1() {
|
||||
fxt.Init__root
|
||||
( fxt.Make_mock(0).Add_subs("subs1"
|
||||
, fxt.Make_mock(1).Add_prop("prop1", "a").Add_subs("subs2"
|
||||
, fxt.Make_mock(11).Add_prop("prop2", "1")
|
||||
, fxt.Make_mock(12).Add_prop("prop2", "2"))
|
||||
));
|
||||
fxt.Test__parse
|
||||
( "{{#subs1}}{{prop1}}{{#subs2}}{{prop2}}{{/subs2}}{{/subs1}}"
|
||||
, "a12"
|
||||
);
|
||||
}
|
||||
@Test public void Section_subs_nest_2() {
|
||||
fxt.Init__root
|
||||
( fxt.Make_mock(0).Add_subs("subs1"
|
||||
, fxt.Make_mock(1).Add_prop("prop1", "a").Add_subs("subs2"
|
||||
, fxt.Make_mock(11).Add_prop("prop2", "1")
|
||||
, fxt.Make_mock(12).Add_prop("prop2", "2")
|
||||
)
|
||||
, fxt.Make_mock(2).Add_prop("prop1", "b")
|
||||
)
|
||||
);
|
||||
fxt.Test__parse
|
||||
( "{{#subs1}}{{prop1}}{{#subs2}}{{prop2}}{{/subs2}}{{/subs1}}"
|
||||
, "a12b"
|
||||
);
|
||||
}
|
||||
@Test public void Section_subs_nest_3() {
|
||||
fxt.Init__root
|
||||
( fxt.Make_mock(0).Add_subs("subs1"
|
||||
, fxt.Make_mock(1).Add_prop("prop1", "a").Add_subs("subs2"
|
||||
, fxt.Make_mock(11).Add_prop("prop2", "1")
|
||||
, fxt.Make_mock(12).Add_prop("prop2", "2")
|
||||
)
|
||||
, fxt.Make_mock(2).Add_prop("prop1", "b").Add_subs("subs2"
|
||||
, fxt.Make_mock(21).Add_prop("prop2", "3")
|
||||
, fxt.Make_mock(22).Add_prop("prop2", "4")
|
||||
)
|
||||
)
|
||||
);
|
||||
fxt.Test__parse
|
||||
( "{{#subs1}}{{prop1}}{{#subs2}}{{prop2}}{{/subs2}}{{prop1}}{{/subs1}}"
|
||||
, "a12ab34b"
|
||||
);
|
||||
}
|
||||
}
|
||||
class Mustache_itm_render_fxt {
|
||||
private final Mustache_tkn_parser parser = new Mustache_tkn_parser();
|
||||
private final Mustache_render_ctx ctx = new Mustache_render_ctx();
|
||||
private final Bry_bfr tmp_bfr = Bry_bfr.new_();
|
||||
private Mustache_doc_itm__mock root;
|
||||
public Mustache_doc_itm__mock Make_mock(int id) {return new Mustache_doc_itm__mock(id);}
|
||||
public void Init__root(Mustache_doc_itm__mock v) {this.root = v;}
|
||||
public void Test__parse(String src_str, String expd) {
|
||||
byte[] src_bry = Bry_.new_a7(src_str);
|
||||
Mustache_tkn_itm actl_itm = parser.Parse(src_bry, 0, src_bry.length);
|
||||
ctx.Init(root);
|
||||
actl_itm.Render(tmp_bfr, ctx);
|
||||
Tfds.Eq_str_lines(expd, tmp_bfr.To_str_and_clear());
|
||||
}
|
||||
}
|
||||
class Mustache_doc_itm__mock implements Mustache_doc_itm {
|
||||
private final Hash_adp hash_prop = Hash_adp_.new_(), hash_bool = Hash_adp_.new_(), hash_subs = Hash_adp_.new_();
|
||||
public Mustache_doc_itm__mock(int id) {this.id = id;}
|
||||
public int id;
|
||||
public Mustache_doc_itm__mock Add_prop(String key, String val) {hash_prop.Add(key, Bry_.new_u8(val)); return this;}
|
||||
public Mustache_doc_itm__mock Add_bool_y(String key) {hash_bool.Add(key, Bool_obj_ref.y_()); return this;}
|
||||
public Mustache_doc_itm__mock Add_bool_n(String key) {hash_bool.Add(key, Bool_obj_ref.n_()); return this;}
|
||||
public Mustache_doc_itm__mock Add_subs(String key, Mustache_doc_itm__mock... ary) {hash_subs.Add(key, ary); return this;}
|
||||
public byte[] Get_prop(String key) {
|
||||
byte[] rv = (byte[])hash_prop.Get_by(key);
|
||||
return rv == null ? Mustache_doc_itm_.Null_val : rv;
|
||||
}
|
||||
public Mustache_doc_itm[] Get_subs(String key) {
|
||||
Object rv = hash_bool.Get_by(key);
|
||||
if (rv != null) {
|
||||
boolean bool_val = ((Bool_obj_ref)rv).Val();
|
||||
return bool_val ? Mustache_doc_itm_.Ary__bool__y : Mustache_doc_itm_.Ary__bool__n;
|
||||
}
|
||||
return (Mustache_doc_itm__mock[])hash_subs.Get_by(key);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
class Mustache_tkn_def {
|
||||
public byte[] Variable_lhs = Dflt_variable_lhs;
|
||||
public byte[] Variable_rhs = Dflt_variable_rhs;
|
||||
public int Variable_lhs_len;
|
||||
public int Variable_rhs_len;
|
||||
public static final byte[]
|
||||
Dflt_variable_lhs = Bry_.new_a7("{{")
|
||||
, Dflt_variable_rhs = Bry_.new_a7("}}")
|
||||
;
|
||||
public static final byte
|
||||
Variable = Byte_ascii.Curly_end // {{=<% %>=}}
|
||||
, Escape_bgn = Byte_ascii.Curly_bgn // {{{escape}}}
|
||||
, Escape_end = Byte_ascii.Curly_end // {{{escape}}}
|
||||
, Section = Byte_ascii.Hash // {{#section}}
|
||||
, Grp_end = Byte_ascii.Slash // {{/section}}
|
||||
, Inverted = Byte_ascii.Pow // {{^inverted}}
|
||||
, Comment = Byte_ascii.Bang // {{!comment}}
|
||||
, Partial = Byte_ascii.Angle_bgn // {{>partial}}
|
||||
, Delimiter_bgn = Byte_ascii.Eq // {{=<% %>=}}
|
||||
, Delimiter_end = Byte_ascii.Curly_end // {{=<% %>=}}
|
||||
;
|
||||
public Mustache_tkn_def() {
|
||||
Variable_lhs_len = Variable_lhs.length;
|
||||
Variable_rhs_len = Variable_rhs.length;
|
||||
}
|
||||
}
|
@ -0,0 +1,111 @@
|
||||
/*
|
||||
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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
public interface Mustache_tkn_itm {
|
||||
int Tid();
|
||||
String Key();
|
||||
Mustache_tkn_itm[] Subs_ary();
|
||||
void Subs_ary_(Mustache_tkn_itm[] v);
|
||||
void Render(Bry_bfr bfr, Mustache_render_ctx ctx);
|
||||
}
|
||||
class Mustache_tkn_itm_ {// for types, see http://mustache.github.io/mustache.5.html
|
||||
public static final int Tid__root = 0, Tid__text = 1, Tid__variable = 2, Tid__escape = 3, Tid__section = 4, Tid__inverted = 5, Tid__comment = 6, Tid__partial = 7, Tid__delimiter = 8;
|
||||
public static final Mustache_tkn_itm[] Ary_empty = new Mustache_tkn_itm[0];
|
||||
}
|
||||
abstract class Mustache_tkn_base implements Mustache_tkn_itm {
|
||||
public Mustache_tkn_base(int tid, byte[] key_bry) {this.tid = tid; this.key = String_.new_u8(key_bry);}
|
||||
public int Tid() {return tid;} private final int tid;
|
||||
public String Key() {return key;} private final String key;
|
||||
@gplx.Virtual public Mustache_tkn_itm[] Subs_ary() {return Mustache_tkn_itm_.Ary_empty;}
|
||||
@gplx.Virtual public void Subs_ary_(Mustache_tkn_itm[] v) {throw Err_.new_unsupported();} // fail if trying to set and not overridden
|
||||
@gplx.Virtual public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {throw Err_.new_unsupported();} // should be abstract
|
||||
}
|
||||
class Mustache_tkn_root extends Mustache_tkn_base { // EX: {{variable}} -> <a>
|
||||
private Mustache_tkn_itm[] subs_ary;
|
||||
public Mustache_tkn_root() {super(Mustache_tkn_itm_.Tid__root, Bry_.Empty);}
|
||||
@Override public Mustache_tkn_itm[] Subs_ary() {return subs_ary;}
|
||||
@Override public void Subs_ary_(Mustache_tkn_itm[] v) {subs_ary = v;}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
int subs_len = subs_ary.length;
|
||||
for (int i = 0; i < subs_len; ++i) {
|
||||
Mustache_tkn_itm sub = subs_ary[i];
|
||||
sub.Render(bfr, ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_text extends Mustache_tkn_base { // EX: text -> text
|
||||
private final byte[] src; private final int src_bgn, src_end;
|
||||
public Mustache_tkn_text(byte[] src, int src_bgn, int src_end) {super(Mustache_tkn_itm_.Tid__text, Bry_.Empty);
|
||||
this.src = src;
|
||||
this.src_bgn = src_bgn;
|
||||
this.src_end = src_end;
|
||||
}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
bfr.Add_mid(src, src_bgn, src_end);
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_comment extends Mustache_tkn_base { // EX: {{!section}}comment{{/section}} ->
|
||||
public Mustache_tkn_comment() {super(Mustache_tkn_itm_.Tid__comment, Bry_.Empty);}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {}
|
||||
}
|
||||
class Mustache_tkn_variable extends Mustache_tkn_base { // EX: {{variable}} -> <a>
|
||||
public Mustache_tkn_variable(byte[] key) {super(Mustache_tkn_itm_.Tid__variable, key);}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
String key = this.Key();
|
||||
byte[] val = ctx.Render_variable(key);
|
||||
if (val != Mustache_doc_itm_.Null_val) // if not found, return empty String by default
|
||||
gplx.langs.htmls.Gfh_utl.Escape_html_to_bfr(bfr, val, 0, val.length, Bool_.Y, Bool_.Y, Bool_.Y, Bool_.Y, Bool_.Y);
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_escape extends Mustache_tkn_base { // EX: {{{variable}}} -> <a>
|
||||
public Mustache_tkn_escape(byte[] key) {super(Mustache_tkn_itm_.Tid__escape, key);}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
String key = this.Key();
|
||||
byte[] val = ctx.Render_variable(key);
|
||||
if (val != Mustache_doc_itm_.Null_val) // if not found, return empty String by default
|
||||
bfr.Add(val);
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_section extends Mustache_tkn_base { // EX: {{#section}}val{{/section}} -> val (if boolean) or valvalval (if list)
|
||||
private Mustache_tkn_itm[] subs_ary;
|
||||
public Mustache_tkn_section(byte[] key) {super(Mustache_tkn_itm_.Tid__section, key);}
|
||||
@Override public Mustache_tkn_itm[] Subs_ary() {return subs_ary;}
|
||||
@Override public void Subs_ary_(Mustache_tkn_itm[] v) {subs_ary = v;}
|
||||
@Override public void Render(Bry_bfr bfr, Mustache_render_ctx ctx) {
|
||||
String key = this.Key();
|
||||
Mustache_tkn_itm[] subs_ary = this.Subs_ary();
|
||||
ctx.Section_bgn(key);
|
||||
while (ctx.Section_do()) {
|
||||
int subs_len = subs_ary.length;
|
||||
for (int i = 0; i < subs_len; ++i) {
|
||||
Mustache_tkn_itm sub = subs_ary[i];
|
||||
sub.Render(bfr, ctx);
|
||||
}
|
||||
}
|
||||
ctx.Section_end();
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_inverted extends Mustache_tkn_base { // EX: {{^section}}missing{{/section}} -> missing
|
||||
public Mustache_tkn_inverted(byte[] key) {super(Mustache_tkn_itm_.Tid__inverted, key);}
|
||||
}
|
||||
class Mustache_tkn_partial extends Mustache_tkn_base { // EX: {{>a}} -> abc (deferred eval)
|
||||
public Mustache_tkn_partial(byte[] key) {super(Mustache_tkn_itm_.Tid__partial, key);}
|
||||
}
|
||||
class Mustache_tkn_delimiter extends Mustache_tkn_base {// EX: {{=<% %>=}} -> <% variable %>
|
||||
public Mustache_tkn_delimiter(byte[] key) {super(Mustache_tkn_itm_.Tid__delimiter, key);}
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
/*
|
||||
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.langs.mustaches; import gplx.*; import gplx.langs.*;
|
||||
public class Mustache_tkn_parser {
|
||||
private byte[] src; private int src_end;
|
||||
private final Mustache_tkn_def tkn_def = new Mustache_tkn_def();
|
||||
public Mustache_tkn_itm Parse(byte[] src, int src_bgn, int src_end) {
|
||||
this.src = src; this.src_end = src_end;
|
||||
Mustache_tkn_root root = new Mustache_tkn_root();
|
||||
Parse_grp(root, src_bgn);
|
||||
return root;
|
||||
}
|
||||
private int Parse_grp(Mustache_tkn_itm owner, int src_bgn) {
|
||||
List_adp subs_list = List_adp_.new_();
|
||||
int txt_bgn = src_bgn;
|
||||
boolean end_grp = false;
|
||||
while (true) {// loop for "{{"
|
||||
int lhs_bgn = Bry_find_.Find_fwd(src, tkn_def.Variable_lhs, txt_bgn, src_end); // next "{{"
|
||||
if (lhs_bgn == Bry_find_.Not_found) { // no more "{{"
|
||||
subs_list.Add(new Mustache_tkn_text(src, txt_bgn, src_end)); // add everything between prv "}}" and cur "{{"
|
||||
break;
|
||||
}
|
||||
int lhs_end = lhs_bgn + tkn_def.Variable_lhs_len;
|
||||
|
||||
Mustache_tkn_data tkn_data = new Mustache_tkn_data(src[lhs_end]); // preview tkn
|
||||
lhs_end += tkn_data.lhs_end_adj;
|
||||
|
||||
int rhs_bgn = Bry_find_.Find_fwd(src, tkn_def.Variable_rhs, lhs_end, src_end); // next "}}"
|
||||
if (rhs_bgn == Bry_find_.Not_found) throw Fail(lhs_bgn, "unclosed tag"); // fail if no "}}"
|
||||
int rhs_end = rhs_bgn + tkn_def.Variable_rhs_len;
|
||||
if (tkn_data.rhs_bgn_chk != Byte_ascii.Null) {
|
||||
if (src[rhs_bgn] != tkn_data.rhs_bgn_chk) throw Fail(lhs_end, "invalid check byte");
|
||||
++rhs_end; // skip the chk_byte; note that bottom of function will skip "}}" by adding +2
|
||||
}
|
||||
|
||||
|
||||
int txt_end = lhs_bgn; // get text tkn
|
||||
if (tkn_data.ws_ignore) {
|
||||
int new_txt_bgn = Trim_bwd_to_nl(src, txt_bgn, txt_end);
|
||||
if (new_txt_bgn != -1) {
|
||||
int new_txt_end = Trim_fwd_to_nl(src, rhs_end, src_end);
|
||||
if (new_txt_end != -1) {
|
||||
txt_end = new_txt_bgn;
|
||||
rhs_end = new_txt_end == src_end ? src_end : new_txt_end + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (txt_end > txt_bgn) // ignore 0-byte text tkns; occurs when consecutive tkns; EX: {{v1}}{{v2}} will try to create text tkn between "}}{{"
|
||||
subs_list.Add(new Mustache_tkn_text(src, txt_bgn, txt_end)); // add everything between prv "}}" and cur "{{"
|
||||
|
||||
txt_bgn = Parse_itm(tkn_data, subs_list, lhs_end, rhs_bgn, rhs_end); // do parse
|
||||
if (txt_bgn < 0) { // NOTE: txt_bgn < 0 means end grp
|
||||
txt_bgn *= -1;
|
||||
end_grp = true;
|
||||
}
|
||||
if (end_grp) break;
|
||||
}
|
||||
if (subs_list.Count() > 0) // don't create subs if no members
|
||||
owner.Subs_ary_((Mustache_tkn_itm[])subs_list.To_ary_and_clear(Mustache_tkn_itm.class));
|
||||
return txt_bgn;
|
||||
}
|
||||
private int Parse_itm(Mustache_tkn_data tkn_data, List_adp subs_list, int lhs_end, int rhs_bgn, int rhs_end) {
|
||||
byte[] val_bry = Bry_.Mid(src, lhs_end, rhs_bgn);
|
||||
Mustache_tkn_base tkn = null;
|
||||
switch (tkn_data.tid) {
|
||||
default: throw Err_.new_unhandled(tkn_data.tid);
|
||||
case Mustache_tkn_def.Variable: tkn = new Mustache_tkn_variable(val_bry); break;
|
||||
case Mustache_tkn_def.Comment: tkn = new Mustache_tkn_comment(); break;
|
||||
case Mustache_tkn_def.Partial: tkn = new Mustache_tkn_partial(val_bry); break;
|
||||
case Mustache_tkn_def.Delimiter_bgn: tkn = new Mustache_tkn_delimiter(val_bry); break; // TODO: implement delimiter; EX: {{=<% %>=}}
|
||||
case Mustache_tkn_def.Escape_bgn: tkn = new Mustache_tkn_escape(val_bry); break;
|
||||
case Mustache_tkn_def.Section: tkn = new Mustache_tkn_section(val_bry); break;
|
||||
case Mustache_tkn_def.Inverted: tkn = new Mustache_tkn_inverted(val_bry); break;
|
||||
case Mustache_tkn_def.Grp_end: {
|
||||
return -(rhs_end); // pop the stack
|
||||
}
|
||||
}
|
||||
subs_list.Add(tkn);
|
||||
if (tkn_data.parse_grp) {
|
||||
return Parse_grp(tkn, rhs_end);
|
||||
}
|
||||
else
|
||||
return rhs_end;
|
||||
}
|
||||
private Err Fail(int pos, String fmt, Object... args) {
|
||||
return Err_.new_("mustache", fmt, "excerpt", Bry_.Mid_by_len_safe(src, pos, 32));
|
||||
}
|
||||
private static int Trim_bwd_to_nl(byte[] src, int txt_bgn, int txt_end) {
|
||||
int stop = txt_bgn - 1;
|
||||
int pos = txt_end - 1;
|
||||
while (pos > stop) {
|
||||
byte b = src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Tab:
|
||||
case Byte_ascii.Space: --pos; break;
|
||||
case Byte_ascii.Nl: return pos + 1; // 1 char after \n
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
private static int Trim_fwd_to_nl(byte[] src, int txt_bgn, int txt_end) {
|
||||
int pos = txt_bgn;
|
||||
while (pos < txt_end) {
|
||||
byte b = src[pos];
|
||||
switch (b) {
|
||||
case Byte_ascii.Tab:
|
||||
case Byte_ascii.Space: ++pos; break;
|
||||
case Byte_ascii.Nl: return pos;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
class Mustache_tkn_data {
|
||||
public int tid;
|
||||
public int lhs_end_adj;
|
||||
public byte rhs_bgn_chk;
|
||||
public boolean parse_grp;
|
||||
public boolean ws_ignore;
|
||||
public Mustache_tkn_data(byte b) {
|
||||
tid = b;
|
||||
parse_grp = ws_ignore = false;
|
||||
lhs_end_adj = 1;
|
||||
rhs_bgn_chk = Byte_ascii.Null;
|
||||
switch (b) {
|
||||
default: lhs_end_adj = 0; tid = Mustache_tkn_def.Variable; break;
|
||||
case Mustache_tkn_def.Comment:
|
||||
case Mustache_tkn_def.Partial:
|
||||
case Mustache_tkn_def.Grp_end: ws_ignore = true; break;
|
||||
case Mustache_tkn_def.Delimiter_bgn: rhs_bgn_chk = Mustache_tkn_def.Delimiter_end; break; // check for "=}}"; TODO: implement delimiter; EX: {{=<% %>=}}
|
||||
case Mustache_tkn_def.Escape_bgn: rhs_bgn_chk = Mustache_tkn_def.Escape_end; break; // check for ""
|
||||
case Mustache_tkn_def.Section:
|
||||
case Mustache_tkn_def.Inverted: ws_ignore = true; parse_grp = true; break;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
/*
|
||||
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.bldrs.cmds.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.bldrs.*; import gplx.xowa.bldrs.cmds.*;
|
||||
public class Xob_rnd_cmd implements Xob_cmd {
|
||||
// private final Xob_bldr bldr; private final Xowe_wiki wiki;
|
||||
public Xob_rnd_cmd(Xob_bldr bldr, Xowe_wiki wiki) {}//this.bldr = bldr; this.wiki = wiki;}
|
||||
public String Cmd_key() {return Xob_cmd_keys.Key_util_random;}
|
||||
public void Cmd_run() {
|
||||
}
|
||||
public Object Invk(GfsCtx ctx, int ikey, String k, GfoMsg m) {return this;}
|
||||
|
||||
public void Cmd_init(Xob_bldr bldr) {}
|
||||
public void Cmd_bgn(Xob_bldr bldr) {}
|
||||
public void Cmd_end() {}
|
||||
public void Cmd_term() {}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
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.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
|
||||
public class Rndm_core_row {
|
||||
public Rndm_core_row(int type, int total, int interval) {this.type = type; this.total = total; this.interval = interval;}
|
||||
public final int type;
|
||||
public final int total;
|
||||
public final int interval;
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*
|
||||
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.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
|
||||
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
|
||||
public class Rndm_core_tbl implements Rls_able {
|
||||
private final String tbl_name = "rndm_core"; private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
|
||||
private final String fld_rndm_uid, fld_rndm_total, fld_rndm_interval;
|
||||
private final Db_conn conn; private Db_stmt stmt_select, stmt_insert;
|
||||
public Rndm_core_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
fld_rndm_uid = flds.Add_int("rndm_uid");
|
||||
fld_rndm_total = flds.Add_int("rndm_total");
|
||||
fld_rndm_interval = flds.Add_int("rndm_interval");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_name(tbl_name, "core", fld_rndm_uid)));}
|
||||
public void Insert(int uid, int total, int interval) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear().Val_int(fld_rndm_uid, uid).Val_int(fld_rndm_total, total).Val_int(interval).Exec_insert();
|
||||
}
|
||||
public Rndm_core_row Select(int uid) {
|
||||
if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, flds, fld_rndm_uid);
|
||||
Db_rdr rdr = stmt_select.Clear().Val_int(fld_rndm_uid, uid).Exec_select__rls_manual();
|
||||
try {
|
||||
int total = rdr.Read_int(fld_rndm_total);
|
||||
int interval = rdr.Read_int(fld_rndm_interval);
|
||||
return new Rndm_core_row(uid, total, interval);
|
||||
} finally {rdr.Rls();}
|
||||
}
|
||||
public void Rls() {
|
||||
stmt_select = Db_stmt_.Rls(stmt_select);
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
/*
|
||||
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.specials.randoms; import gplx.*; import gplx.xowa.*; import gplx.xowa.specials.*;
|
||||
import gplx.core.ios.*; import gplx.dbs.*; import gplx.dbs.utls.*;
|
||||
public class Rndm_range_tbl implements Rls_able {
|
||||
private final String tbl_name = "rng_range"; private final Dbmeta_fld_list flds = Dbmeta_fld_list.new_();
|
||||
private final String fld_rng_uid, fld_rng_idx, fld_rng_page_id;
|
||||
private final Db_conn conn; private Db_stmt stmt_select, stmt_insert;
|
||||
public Rndm_range_tbl(Db_conn conn) {
|
||||
this.conn = conn;
|
||||
fld_rng_uid = flds.Add_int("rng_uid");
|
||||
fld_rng_idx = flds.Add_int("rng_idx");
|
||||
fld_rng_page_id = flds.Add_int("rng_page_id");
|
||||
conn.Rls_reg(this);
|
||||
}
|
||||
public void Create_tbl() {conn.Ddl_create_tbl(Dbmeta_tbl_itm.New(tbl_name, flds, Dbmeta_idx_itm.new_unique_by_name(tbl_name, "core", fld_rng_uid, fld_rng_idx)));}
|
||||
public void Insert(int uid, int idx, int page_id) {
|
||||
if (stmt_insert == null) stmt_insert = conn.Stmt_insert(tbl_name, flds);
|
||||
stmt_insert.Clear().Val_int(fld_rng_uid, uid).Val_int(fld_rng_idx, idx).Val_int(fld_rng_page_id, page_id).Exec_insert();
|
||||
}
|
||||
public void Select(int type) {
|
||||
// if (stmt_select == null) stmt_select = conn.Stmt_select(tbl_name, flds, fld_rng_uid);
|
||||
// Db_rdr rdr = stmt_select.Clear().Val_int(fld_rng_type, type).Exec_select__rls_manual();
|
||||
// try {
|
||||
// int total = rdr.Read_int(fld_rng_total);
|
||||
// int interval = rdr.Read_int(fld_rng_interval);
|
||||
// return new Rndm_core_row(type, total, interval);
|
||||
// } finally {rdr.Rls();}
|
||||
}
|
||||
public void Rls() {
|
||||
stmt_select = Db_stmt_.Rls(stmt_select);
|
||||
stmt_insert = Db_stmt_.Rls(stmt_insert);
|
||||
}
|
||||
public void Get_random(int uid) {
|
||||
// Rndm_core_row core_row = core_tbl.Get(uid);
|
||||
// int rnd = RandomAdp_.new_().Next(core_row.total);
|
||||
// int idx = rnd / core_row.interval;
|
||||
// int off = rnd % core_row.interval;
|
||||
// Rndm_rng_row rng_row = rng_tbl.Get(uid, idx);
|
||||
// Xowd_page page = page_tbl.Get_by_page_offset(rng_row.page_id_bgn, off);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
/*
|
||||
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.flaggedRevs; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.flaggedRevs.scribunto.*;
|
||||
public class Flagged_revs_xtn_mgr extends Xox_mgr_base {
|
||||
@Override public byte[] Xtn_key() {return XTN_KEY;} public static final byte[] XTN_KEY = Bry_.new_a7("FlaggedRevs");
|
||||
@Override public Xox_mgr Clone_new() {return new Flagged_revs_xtn_mgr();}
|
||||
@Override public void Xtn_init_by_app(Xoae_app app) {
|
||||
Scrib_xtn_mgr scrib_xtn = (Scrib_xtn_mgr)app.Xtn_mgr().Get_or_fail(Scrib_xtn_mgr.XTN_KEY);
|
||||
scrib_xtn.Lib_mgr().Add(new Flagged_revs_lib());
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
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.flaggedRevs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.flaggedRevs.*;
|
||||
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*;
|
||||
import gplx.xowa.xtns.pfuncs.exprs.*;
|
||||
public class Flagged_revs_lib implements Scrib_lib {
|
||||
private Scrib_core core;
|
||||
public Scrib_lua_mod Mod() {return mod;} private Scrib_lua_mod mod;
|
||||
public Scrib_lib Init() {procs.Init_by_lib(this, Proc_names); return this;}
|
||||
public Scrib_lib Core_(Scrib_core v) {this.core = v; return this;} // TEST:
|
||||
public Scrib_lua_mod Register(Scrib_core core, Io_url script_dir) {
|
||||
this.core = core;
|
||||
Init();
|
||||
mod = core.RegisterInterface(this, core.App().Fsys_mgr().Bin_xtns_dir().GenSubFil_nest("FlaggedRevs", "scribunto", "mw.ext.FlaggedRevs.lua"));
|
||||
return mod;
|
||||
}
|
||||
public Scrib_proc_mgr Procs() {return procs;} private final Scrib_proc_mgr procs = new Scrib_proc_mgr();
|
||||
public boolean Procs_exec(int key, Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
switch (key) {
|
||||
case Proc_getStabilitySettings: return GetStabilitySettings(args, rslt);
|
||||
default: throw Err_.new_unhandled(key);
|
||||
}
|
||||
}
|
||||
private static final int Proc_getStabilitySettings = 0;
|
||||
public static final String Invk_getStabilitySettings = "getStabilitySettings";
|
||||
private static final String[] Proc_names = String_.Ary(Invk_getStabilitySettings);
|
||||
public boolean GetStabilitySettings(Scrib_proc_args args, Scrib_proc_rslt rslt) {
|
||||
byte[] page_name = args.Cast_bry_or_null(0);
|
||||
Xoa_ttl page_ttl = null;
|
||||
if (page_name == null)
|
||||
page_ttl = core.Ctx().Page().Ttl();
|
||||
else {
|
||||
page_ttl = core.Wiki().Ttl_parse(page_name);
|
||||
if (page_ttl == null) return rslt.Init_null();
|
||||
}
|
||||
// if ( !FlaggedRevs::inReviewNamespace( $title ) ) return rslt.Init_null();
|
||||
return rslt.Init_obj(getDefaultVisibilitySettings());
|
||||
}
|
||||
private static KeyVal[] getDefaultVisibilitySettings() {
|
||||
KeyVal[] rv = new KeyVal[3];
|
||||
rv[0] = KeyVal_.new_("over"+"ride", 0); // FlaggedRevs::isStableShownByDefault() ? 1 : 0,
|
||||
rv[1] = KeyVal_.new_("autoreview", "");
|
||||
rv[2] = KeyVal_.new_("expiry", "infinity");
|
||||
return rv;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
/*
|
||||
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.flaggedRevs.scribunto; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; import gplx.xowa.xtns.flaggedRevs.*;
|
||||
import org.junit.*;
|
||||
import gplx.xowa.xtns.scribunto.*; import gplx.xowa.xtns.scribunto.libs.*; import gplx.xowa.xtns.scribunto.engines.mocks.*;
|
||||
public class Flagged_revs_lib_tst {
|
||||
private final Mock_scrib_fxt fxt = new Mock_scrib_fxt(); private Flagged_revs_lib lib;
|
||||
@Before public void init() {
|
||||
fxt.Clear();
|
||||
lib = new Flagged_revs_lib();
|
||||
lib.Init();
|
||||
lib.Core_(fxt.Core());
|
||||
}
|
||||
@Test public void GetStabilitySettings() {
|
||||
fxt.Test__proc__objs__nest(lib, Flagged_revs_lib.Invk_getStabilitySettings, Object_.Ary("Page1"), String_.Concat_lines_nl_skip_last
|
||||
( "1="
|
||||
, " over"+"ride=0"
|
||||
, " autoreview="
|
||||
, " expiry=infinity"
|
||||
));
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
/*
|
||||
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.langs.mustaches.*;
|
||||
public class Pgbnr_icon implements Mustache_doc_itm {
|
||||
public byte[] name;
|
||||
public byte[] title;
|
||||
public byte[] url;
|
||||
public Pgbnr_icon(byte[] name, byte[] title, byte[] url) {this.name = name; this.title = title; this.url = url;}
|
||||
public byte[] Get_prop(String key) {
|
||||
if (String_.Eq(key, "name")) return name;
|
||||
else if (String_.Eq(key, "title")) return title;
|
||||
else if (String_.Eq(key, "url")) return url;
|
||||
else if (String_.Eq(key, "html")) return Mustache_doc_itm_.Null_val;
|
||||
return Mustache_doc_itm_.Null_val;
|
||||
}
|
||||
public Mustache_doc_itm[] Get_subs(String key) {return Mustache_doc_itm_.Ary__empty;}
|
||||
public static final Pgbnr_icon[] Ary_empty = new Pgbnr_icon[0];
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
/*
|
||||
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.langs.mustaches.*;
|
||||
public class Pgbnr_itm implements Mustache_doc_itm {
|
||||
public byte[] name, toc;
|
||||
private byte[] tooltip, title, originx, bannerfile, banner, srcset;
|
||||
private double data_pos_x, data_pos_y;
|
||||
private int maxWidth;
|
||||
private boolean bottomtoc, isHeadingOverrideEnabled;
|
||||
private Pgbnr_icon[] icons;
|
||||
public void Init_from_wtxt(byte[] name, byte[] tooltip, byte[] title, boolean bottomtoc, byte[] toc, double data_pos_x, double data_pos_y, byte[] originx, Pgbnr_icon[] icons) {
|
||||
this.name = name;
|
||||
this.tooltip = tooltip; this.title = title; this.bottomtoc = bottomtoc; this.toc = toc; this.icons = icons;
|
||||
this.data_pos_x = data_pos_x; this.data_pos_y = data_pos_y; this.originx = originx;
|
||||
}
|
||||
public void Init_from_html(int maxWidth, byte[] bannerfile, byte[] banner, byte[] srcset, boolean isHeadingOverrideEnabled) {
|
||||
this.maxWidth = maxWidth;
|
||||
this.bannerfile = bannerfile;
|
||||
this.banner = banner;
|
||||
this.srcset = srcset;
|
||||
this.isHeadingOverrideEnabled = isHeadingOverrideEnabled;
|
||||
}
|
||||
public byte[] Get_prop(String key) {
|
||||
if (String_.Eq(key, "title")) return title;
|
||||
else if (String_.Eq(key, "tooltip")) return tooltip;
|
||||
else if (String_.Eq(key, "bannerfile")) return bannerfile;
|
||||
else if (String_.Eq(key, "banner")) return banner;
|
||||
else if (String_.Eq(key, "srcset")) return srcset;
|
||||
else if (String_.Eq(key, "originx")) return originx;
|
||||
else if (String_.Eq(key, "data-pos-x")) return Bry_.new_a7(Double_.To_str(data_pos_x));
|
||||
else if (String_.Eq(key, "data-pos-y")) return Bry_.new_a7(Double_.To_str(data_pos_y));
|
||||
else if (String_.Eq(key, "maxWidth")) return Int_.To_bry(maxWidth);
|
||||
else if (String_.Eq(key, "toc")) return toc;
|
||||
return Mustache_doc_itm_.Null_val;
|
||||
}
|
||||
public Mustache_doc_itm[] Get_subs(String key) {
|
||||
if (String_.Eq(key, "icons")) return icons;
|
||||
else if (String_.Eq(key, "hasIcons")) return Mustache_doc_itm_.Ary__bool(icons.length > 0);
|
||||
else if (String_.Eq(key, "bottomtoc")) return Mustache_doc_itm_.Ary__bool(bottomtoc);
|
||||
else if (String_.Eq(key, "isHeadingOverrideEnabled")) return Mustache_doc_itm_.Ary__bool(isHeadingOverrideEnabled);
|
||||
return Mustache_doc_itm_.Ary__empty;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
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.pagebanners; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.brys.*; import gplx.langs.mustaches.*; import gplx.xowa.parsers.*;
|
||||
public class Pgbnr_xtn_mgr extends Xox_mgr_base implements Bfr_arg {
|
||||
@Override public byte[] Xtn_key() {return Xtn_key_static;} public static final byte[] Xtn_key_static = Bry_.new_a7("pagebanner");
|
||||
@Override public Xox_mgr Clone_new() {return new Pgbnr_xtn_mgr();}
|
||||
public Pgbnr_cfg Cfg() {return cfg;} private final Pgbnr_cfg cfg = new Pgbnr_cfg();
|
||||
public Mustache_tkn_itm Template_root() {
|
||||
if (template_root == null) {
|
||||
Mustache_tkn_parser parser = new Mustache_tkn_parser();
|
||||
template_root = parser.Parse(Template_dflt, 0, Template_dflt.length);
|
||||
}
|
||||
return template_root;
|
||||
} private Mustache_tkn_itm template_root;
|
||||
@Override public void Xtn_init_by_app(Xoae_app app) {
|
||||
}
|
||||
public Bfr_arg Write_html(Xop_ctx pctx, Xoae_page page) {
|
||||
this.pctx = pctx; this.page = page;
|
||||
return this;
|
||||
} private Xop_ctx pctx; private Xoae_page page;
|
||||
public void Bfr_arg__add(Bry_bfr bfr) {
|
||||
Pgbnr_itm itm = page.Html_data().Xtn__pgbnr();
|
||||
if (itm == null) return;
|
||||
Pgbnr_func.Add_banner(bfr, pctx);
|
||||
}
|
||||
private static final byte[] Template_dflt = Bry_.New_u8_nl_apos
|
||||
( "<div class='ext-wpb-pagebanner noprint pre-content'>"
|
||||
, " <div class='wpb-topbanner'>"
|
||||
, " {{#isHeadingOverrideEnabled}}<h1 class='wpb-name'>{{title}}</h1>{{/isHeadingOverrideEnabled}}"
|
||||
, " <a class='image' title='{{tooltip}}' href='{{bannerfile}}'><img src='{{banner}}' srcset='{{srcset}}' class='wpb-banner-image {{originx}}' data-pos-x='{{data-pos-x}}' data-pos-y='{{data-pos-y}}' style='max-width:{{maxWidth}}px'></a>"
|
||||
, " {{#hasIcons}}"
|
||||
, " <div class='wpb-iconbox'>"
|
||||
, " {{#icons}}"
|
||||
, " <a href='{{url}}'>{{{html}}}</a>"
|
||||
, " {{/icons}}"
|
||||
, " </div>"
|
||||
, " {{/hasIcons}}"
|
||||
, " </div>"
|
||||
, " <div class='wpb-topbanner-toc {{#bottomtoc}}wpb-bottomtoc{{/bottomtoc}}'><div class='wpb-banner-toc'>{{{toc}}}</div></div>"
|
||||
, "</div>"
|
||||
);
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue