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

Template_styles: Add support for template styles

This commit is contained in:
gnosygnu
2018-10-12 23:16:31 -04:00
parent e78382a8ac
commit a8f6fd0fb0
50 changed files with 1219 additions and 113 deletions

View File

@@ -29,8 +29,11 @@ public class Json_doc_wtr {
public Json_doc_wtr Str(byte[] v) {
if (v == null)
bfr.Add(Object_.Bry__null);
else
bfr.Add_byte(Byte_ascii.Quote).Add(v).Add_byte(Byte_ascii.Quote);
else {
bfr.Add_byte(Byte_ascii.Quote);
bfr.Add_bry_escape(Byte_ascii.Quote, Escaped__quote, v, 0, v.length);
bfr.Add_byte(Byte_ascii.Quote);
}
return this;
}
public Json_doc_wtr Int(int v) {bfr.Add_int_variable(v); return this;}
@@ -94,4 +97,5 @@ public class Json_doc_wtr {
}
public byte[] Bld() {return bfr.To_bry_and_clear();}
public String Bld_as_str() {return bfr.To_str_and_clear();}
private static final byte[] Escaped__quote = Bry_.new_a7("\\\"");
}

View File

@@ -0,0 +1,43 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.langs.jsons; import gplx.*; import gplx.langs.*;
import org.junit.*; import gplx.core.tests.*;
public class Json_doc_wtr_tst {
private final Json_doc_wtr_fxt fxt = new Json_doc_wtr_fxt();
@Test public void Basic() {
fxt.Test__Bld_as_str
( fxt.Exec__Kv_simple("k1", "v\"1")
, fxt.Exec__Concat_apos
( "{"
, " 'k1':'v\\\"1'"
, "}"));
}
}
class Json_doc_wtr_fxt {
public Json_doc_wtr Exec__Kv_simple(String key, String val) {
Json_doc_wtr doc_wtr = new Json_doc_wtr();
doc_wtr.Nde_bgn();
doc_wtr.Kv(Bool_.N, Bry_.new_u8(key), Bry_.new_u8(val));
doc_wtr.Nde_end();
return doc_wtr;
}
public void Test__Bld_as_str(Json_doc_wtr doc_wtr, String expd) {
Gftest.Eq__ary__lines(expd, doc_wtr.Bld_as_str());
}
public String Exec__Concat_apos(String... ary) {
return Json_doc.Make_str_by_apos(ary);
}
}

View File

@@ -22,13 +22,15 @@ NOTE: naive implementation of PHP parser; intended only for parsing Messages**.p
- no functions are supported: EX: strlen('a') fails
*/
public class Php_evaluator implements Php_tkn_wkr {
byte mode = Mode_key_bgn, next_tid = 0, next_mode = 0;
Php_line_assign cur_line; Php_itm_ary cur_ary; Php_key cur_kv_key;
List_adp frame_stack = List_adp_.New();
public Php_evaluator(Gfo_msg_log msg_log) {this.msg_log = msg_log;} Gfo_msg_log msg_log;
private byte mode = Mode_key_bgn, next_tid = 0, next_mode = 0;
private Php_line_assign cur_line; private Php_itm_ary cur_ary; private Php_key cur_kv_key;
private final List_adp frame_stack = List_adp_.New();
public Php_evaluator(Gfo_msg_log msg_log) {this.msg_log = msg_log;} private Gfo_msg_log msg_log;
public void Init(Php_ctx ctx) {src = ctx.Src(); frame_stack.Clear();} private byte[] src;
public List_adp List() {return lines;} List_adp lines = List_adp_.New();
public List_adp List() {return lines;} private final List_adp lines = List_adp_.New();
public Gfo_msg_log Msg_log() {return msg_log;}
public boolean Comments_for_kv() {return comments_for_kv;} public Php_evaluator Comments_for_kv_() {comments_for_kv = true; return this;} private boolean comments_for_kv;
public void Clear() {
lines.Clear(); msg_log.Clear();
cur_line = null;
@@ -40,7 +42,19 @@ public class Php_evaluator implements Php_tkn_wkr {
public void Process(Php_tkn tkn) {
byte tkn_tid = tkn.Tkn_tid();
switch (tkn_tid) {
case Php_tkn_.Tid_declaration: case Php_tkn_.Tid_comment: case Php_tkn_.Tid_ws: // always discard, regardless of mode
case Php_tkn_.Tid_comment:
// ASSUME: comment is end-line comment for the last kv; EX: " a => b, // comment"
if (comments_for_kv && cur_ary != null) {
int subs_len = cur_ary.Subs_len();
if (subs_len > 0) {
Php_itm_kv kv = (Php_itm_kv)cur_ary.Subs_get(subs_len - 1); // get last itm
kv.Comments__add(tkn);
}
}
else
return;
break;
case Php_tkn_.Tid_declaration: case Php_tkn_.Tid_ws: // always discard, regardless of mode
return;
}
switch (mode) {

View File

@@ -17,6 +17,16 @@ package gplx.langs.phps; import gplx.*; import gplx.langs.*;
public class Php_itm_kv implements Php_itm, Php_itm_sub {
public byte Itm_tid() {return Php_itm_.Tid_kv;}
public byte[] Val_obj_bry() {return null;}
public Php_key Key() {return key;} public Php_itm_kv Key_(Php_key v) {this.key = v; return this;} Php_key key;
public Php_itm Val() {return val;} public Php_itm_kv Val_(Php_itm v) {this.val = v; return this;} Php_itm val;
public Php_key Key() {return key;} public Php_itm_kv Key_(Php_key v) {this.key = v; return this;} private Php_key key;
public Php_itm Val() {return val;} public Php_itm_kv Val_(Php_itm v) {this.val = v; return this;} private Php_itm val;
private List_adp comments;
public int Comments__len() {return comments == null ? 0 : comments.Len();}
public Php_tkn_comment Comments__get_at__or_null(int i) {return comments == null ? null : (Php_tkn_comment)comments.Get_at(0);}
public void Comments__add(Php_tkn comment) {
if (comments == null) {
comments = List_adp_.New();
}
comments.Add(comment);
}
}

View File

@@ -22,7 +22,7 @@ interface Php_lxr {
int Lxr_make(Php_ctx ctx, int bgn, int cur);
}
class Php_lxr_ {
public static final byte Tid_declaration = 1, Tid_ws = 2, Tid_comment = 3, Tid_var = 4, Tid_sym = 5, Tid_keyword = 6, Tid_num = 7, Tid_quote = 8;
public static final byte Tid_declaration = 1, Tid_ws = 2, Tid_comment = 3, Tid_var = 4, Tid_sym = 5, Tid_keyword = 6, Tid_num = 7, Tid_quote = 8;
}
abstract class Php_lxr_base implements Php_lxr {
protected byte[] src; protected int src_len; protected Php_tkn_wkr tkn_wkr; protected Php_tkn_factory tkn_factory;
@@ -57,7 +57,7 @@ class Php_lxr_declaration extends Php_lxr_base {
tkn_wkr.Process(tkn_factory.Declaration(bgn, cur));
return cur;
}
private static final byte[] Bry_declaration = Bry_.new_a7("<?php");
private static final byte[] Bry_declaration = Bry_.new_a7("<?php");
}
class Php_lxr_ws extends Php_lxr_base {
public Php_lxr_ws(byte ws_tid) {
@@ -93,7 +93,7 @@ class Php_lxr_ws extends Php_lxr_base {
tkn_wkr.Process(tkn_factory.Ws(bgn, cur, ws_tid));
return cur;
}
public static final byte[] Bry_ws_space = Bry_.new_a7(" "), Bry_ws_nl = Bry_.new_a7("\n"), Bry_ws_tab = Bry_.new_a7("\t"), Bry_ws_cr = Bry_.new_a7("\r");
public static final byte[] Bry_ws_space = Bry_.new_a7(" "), Bry_ws_nl = Bry_.new_a7("\n"), Bry_ws_tab = Bry_.new_a7("\t"), Bry_ws_cr = Bry_.new_a7("\r");
}
class Php_lxr_comment extends Php_lxr_base {
public Php_lxr_comment(byte comment_tid) {
@@ -124,8 +124,8 @@ class Php_lxr_comment extends Php_lxr_base {
tkn_wkr.Process(tkn_factory.Comment(bgn, cur, comment_tid));
return cur;
}
public static final Gfo_msg_itm Dangling_comment = Gfo_msg_itm_.new_warn_(Php_parser.Log_nde, "dangling_comment", "dangling_comment");
public static final byte[] Bry_bgn_mult = Bry_.new_a7("/*"), Bry_bgn_slash = Bry_.new_a7("//"), Bry_bgn_hash = Bry_.new_a7("#")
public static final Gfo_msg_itm Dangling_comment = Gfo_msg_itm_.new_warn_(Php_parser.Log_nde, "dangling_comment", "dangling_comment");
public static final byte[] Bry_bgn_mult = Bry_.new_a7("/*"), Bry_bgn_slash = Bry_.new_a7("//"), Bry_bgn_hash = Bry_.new_a7("#")
, Bry_end_mult = Bry_.new_a7("*/"), Bry_end_nl = Bry_.new_a7("\n");
}
class Php_lxr_var extends Php_lxr_base {
@@ -163,7 +163,7 @@ class Php_lxr_var extends Php_lxr_base {
tkn_wkr.Process(tkn_factory.Var(bgn, cur));
return cur;
}
private static final byte[] Bry_var = Bry_.new_a7("$");
private static final byte[] Bry_var = Bry_.new_a7("$");
}
class Php_lxr_sym extends Php_lxr_base {
public Php_lxr_sym(String hook_str, byte tkn_tid) {this.hook = Bry_.new_a7(hook_str); this.tkn_tid = tkn_tid;} private byte[] hook; byte tkn_tid;
@@ -225,8 +225,8 @@ class Php_lxr_quote extends Php_lxr_base {
tkn_wkr.Process(tkn_factory.Quote(bgn, cur, quote_tid));
return cur;
}
public static final Gfo_msg_itm Dangling_quote = Gfo_msg_itm_.new_warn_(Php_parser.Log_nde, "dangling_quote", "dangling_quote");
public static final byte[] Quote_bry_single = Bry_.new_a7("'"), Quote_bry_double = Bry_.new_a7("\"");
public static final Gfo_msg_itm Dangling_quote = Gfo_msg_itm_.new_warn_(Php_parser.Log_nde, "dangling_quote", "dangling_quote");
public static final byte[] Quote_bry_single = Bry_.new_a7("'"), Quote_bry_double = Bry_.new_a7("\"");
}
class Php_lxr_keyword extends Php_lxr_base {
public Php_lxr_keyword(String hook_str, byte tkn_tid) {this.hook = Bry_.new_a7(hook_str); this.tkn_tid = tkn_tid;} private byte[] hook; byte tkn_tid;

View File

@@ -16,12 +16,14 @@ Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
package gplx.langs.phps; import gplx.*; import gplx.langs.*;
import gplx.core.btries.*; import gplx.core.log_msgs.*;
public class Php_parser {
Php_lxr[] lxrs; int lxrs_len;
int txt_bgn; Php_tkn_txt txt_tkn;
private final Btrie_slim_mgr trie = Btrie_slim_mgr.ci_a7(); // NOTE:ci:PHP tkns are ASCII
private final Btrie_rv trv = new Btrie_rv();
byte[] src; int src_len; Php_tkn_wkr tkn_wkr; Php_tkn_factory tkn_factory = new Php_tkn_factory(); Php_ctx ctx = new Php_ctx();
Php_parser_interrupt[] parser_interrupts = new Php_parser_interrupt[256];
private Php_lxr[] lxrs; private int lxrs_len;
private int txt_bgn; private Php_tkn_txt txt_tkn;
private final Php_tkn_factory tkn_factory = new Php_tkn_factory();
private final Php_ctx ctx = new Php_ctx();
private final Php_parser_interrupt[] parser_interrupts = new Php_parser_interrupt[256];
private int src_len; private Php_tkn_wkr tkn_wkr;
public Php_parser() {
List_adp list = List_adp_.New();
Init_lxr(list, new Php_lxr_declaration());
@@ -57,7 +59,7 @@ public class Php_parser {
}
public void Parse_tkns(String src, Php_tkn_wkr tkn_wkr) {Parse_tkns(Bry_.new_u8(src), tkn_wkr);}
public void Parse_tkns(byte[] src, Php_tkn_wkr tkn_wkr) {
this.src = src; this.src_len = src.length; this.tkn_wkr = tkn_wkr;
this.src_len = src.length; this.tkn_wkr = tkn_wkr;
ctx.Src_(src);
tkn_wkr.Init(ctx);
if (src_len == 0) return;

View File

@@ -23,50 +23,3 @@ class Php_tkn_ {
public static final byte Tid_txt = 1, Tid_declaration = 2, Tid_ws = 3, Tid_comment = 4, Tid_var = 5, Tid_eq = 6, Tid_eq_kv = 7, Tid_semic = 8, Tid_comma = 9, Tid_paren_bgn = 10, Tid_paren_end = 11, Tid_null = 12, Tid_false = 13, Tid_true = 14, Tid_ary = 15, Tid_num = 16, Tid_quote = 17, Tid_brack_bgn = 18, Tid_brack_end = 19;
public static String Xto_str(byte tid) {return Byte_.To_str(tid);}
}
abstract class Php_tkn_base implements Php_tkn {
public abstract byte Tkn_tid();
public int Src_bgn() {return src_bgn;} private int src_bgn;
public int Src_end() {return src_end;} public void Src_end_(int v) {this.src_end = v;} private int src_end;
public void Src_rng_(int src_bgn, int src_end) {this.src_bgn = src_bgn; this.src_end = src_end;}
}
class Php_tkn_generic extends Php_tkn_base {
public Php_tkn_generic(int src_bgn, int src_end, byte tid) {this.Src_rng_(src_bgn, src_end); this.tid = tid;}
@Override public byte Tkn_tid() {return tid;} private byte tid;
}
class Php_tkn_txt extends Php_tkn_base {
public Php_tkn_txt(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_txt;}
}
class Php_tkn_ws extends Php_tkn_base {
public Php_tkn_ws(int src_bgn, int src_end, byte ws_tid) {this.Src_rng_(src_bgn, src_end); this.ws_tid = ws_tid;}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_ws;}
public byte Ws_tid() {return ws_tid;} private byte ws_tid;
public static final byte Tid_space = 0, Tid_nl = 1, Tid_tab = 2, Tid_cr = 3;
}
class Php_tkn_comment extends Php_tkn_base {
public Php_tkn_comment(int src_bgn, int src_end, byte comment_tid) {this.Src_rng_(src_bgn, src_end); this.comment_tid = comment_tid;}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_comment;}
public byte Comment_tid() {return comment_tid;} private byte comment_tid;
public static final byte Tid_null = 0, Tid_mult = 1, Tid_slash = 2, Tid_hash = 3;
}
class Php_tkn_var extends Php_tkn_base {
public Php_tkn_var(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_var;}
public byte[] Var_name(byte[] src) {return Bry_.Mid(src, this.Src_bgn() + 1, this.Src_end());} // NOTE: assume vars are of form $abc; +1 to skip first $
}
class Php_tkn_num extends Php_tkn_base {
public Php_tkn_num(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_num;}
public int Num_val_int(byte[] src) {return Bry_.To_int_or(src, this.Src_bgn(), this.Src_end(), Int_.Min_value);}
}
class Php_tkn_quote extends Php_tkn_base {
public Php_tkn_quote(int src_bgn, int src_end, byte quote_tid) {this.Src_rng_(src_bgn, src_end); this.quote_tid = quote_tid;}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_quote;}
public byte Quote_tid() {return quote_tid;} private byte quote_tid;
public byte[] Quote_text(byte[] src) {return Bry_.Mid(src, this.Src_bgn() + 1, this.Src_end() - 1);} // NOTE: assume quote are of form 'abc'; +1, -1 to skip flanking chars
public static final byte Tid_null = 0, Tid_mult = 1, Tid_slash = 2, Tid_hash = 3;
}
class Php_tkn_declaration extends Php_tkn_base {
@Override public byte Tkn_tid() {return Php_tkn_.Tid_declaration;}
public static final Php_tkn_declaration Instance = new Php_tkn_declaration();
}

View File

@@ -0,0 +1,57 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.langs.phps; import gplx.*; import gplx.langs.*;
public abstract class Php_tkn_base implements Php_tkn {
public abstract byte Tkn_tid();
public int Src_bgn() {return src_bgn;} private int src_bgn;
public int Src_end() {return src_end;} public void Src_end_(int v) {this.src_end = v;} private int src_end;
public void Src_rng_(int src_bgn, int src_end) {this.src_bgn = src_bgn; this.src_end = src_end;}
}
class Php_tkn_generic extends Php_tkn_base {
public Php_tkn_generic(int src_bgn, int src_end, byte tid) {this.Src_rng_(src_bgn, src_end); this.tid = tid;}
@Override public byte Tkn_tid() {return tid;} private byte tid;
}
class Php_tkn_txt extends Php_tkn_base {
public Php_tkn_txt(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_txt;}
}
class Php_tkn_ws extends Php_tkn_base {
public Php_tkn_ws(int src_bgn, int src_end, byte ws_tid) {this.Src_rng_(src_bgn, src_end); this.ws_tid = ws_tid;}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_ws;}
public byte Ws_tid() {return ws_tid;} private byte ws_tid;
public static final byte Tid_space = 0, Tid_nl = 1, Tid_tab = 2, Tid_cr = 3;
}
class Php_tkn_var extends Php_tkn_base {
public Php_tkn_var(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_var;}
public byte[] Var_name(byte[] src) {return Bry_.Mid(src, this.Src_bgn() + 1, this.Src_end());} // NOTE: assume vars are of form $abc; +1 to skip first $
}
class Php_tkn_num extends Php_tkn_base {
public Php_tkn_num(int src_bgn, int src_end) {this.Src_rng_(src_bgn, src_end);}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_num;}
public int Num_val_int(byte[] src) {return Bry_.To_int_or(src, this.Src_bgn(), this.Src_end(), Int_.Min_value);}
}
class Php_tkn_quote extends Php_tkn_base {
public Php_tkn_quote(int src_bgn, int src_end, byte quote_tid) {this.Src_rng_(src_bgn, src_end); this.quote_tid = quote_tid;}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_quote;}
public byte Quote_tid() {return quote_tid;} private byte quote_tid;
public byte[] Quote_text(byte[] src) {return Bry_.Mid(src, this.Src_bgn() + 1, this.Src_end() - 1);} // NOTE: assume quote are of form 'abc'; +1, -1 to skip flanking chars
public static final byte Tid_null = 0, Tid_mult = 1, Tid_slash = 2, Tid_hash = 3;
}
class Php_tkn_declaration extends Php_tkn_base {
@Override public byte Tkn_tid() {return Php_tkn_.Tid_declaration;}
public static final Php_tkn_declaration Instance = new Php_tkn_declaration();
}

View File

@@ -0,0 +1,46 @@
/*
XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3,
or alternatively under the terms of the Apache License Version 2.0.
You may use XOWA according to either of these licenses as is most appropriate
for your project on a case-by-case basis.
The terms of each license can be found in the source code repository:
GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/
package gplx.langs.phps; import gplx.*; import gplx.langs.*;
public class Php_tkn_comment extends Php_tkn_base {
public Php_tkn_comment(int src_bgn, int src_end, byte comment_tid) {this.Src_rng_(src_bgn, src_end); this.comment_tid = comment_tid;}
@Override public byte Tkn_tid() {return Php_tkn_.Tid_comment;}
public byte Comment_tid() {return comment_tid;} private byte comment_tid;
public void To_bfr(Bry_bfr bfr, byte[] src, boolean trim) {
int bgn = this.Src_bgn();
int end = this.Src_end();
switch (comment_tid) {
case Tid_mult: // EX: /* comment */
bgn += 2;
end -= 2;
break;
case Tid_slash: // EX: // comment\n
bgn += 2;
end -= 1;
break;
case Tid_hash: // EX: # comment\n
bgn += 1;
end -= 1;
break;
}
if (trim) {
bgn = Bry_find_.Find_fwd_while_not_ws(src, bgn, end);
end = Bry_find_.Find_bwd__skip_ws(src, end, bgn);
}
bfr.Add_mid(src, bgn, end);
}
public static final byte Tid_null = 0, Tid_mult = 1, Tid_slash = 2, Tid_hash = 3;
}