mirror of
https://github.com/gnosygnu/xowa.git
synced 2026-03-02 03:49:30 +00:00
v2.7.2.1
This commit is contained in:
87
400_xowa/src/gplx/xowa/xtns/syntaxHighlight/Int_rng_mgr.java
Normal file
87
400_xowa/src/gplx/xowa/xtns/syntaxHighlight/Int_rng_mgr.java
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
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.syntaxHighlight; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
public interface Int_rng_mgr {
|
||||
boolean Match(int v);
|
||||
boolean Parse(byte[] src);
|
||||
}
|
||||
class Int_rng_mgr_null implements Int_rng_mgr {
|
||||
public boolean Match(int v) {return false;}
|
||||
public boolean Parse(byte[] src) {return false;}
|
||||
public static final Int_rng_mgr_null _ = new Int_rng_mgr_null(); Int_rng_mgr_null() {}
|
||||
}
|
||||
class Int_rng_mgr_base implements Int_rng_mgr {
|
||||
private List_adp itms = List_adp_.new_();
|
||||
public void Clear() {itms.Clear();}
|
||||
public boolean Match(int v) {
|
||||
int len = itms.Count();
|
||||
for (int i = 0; i < len; i++) {
|
||||
Int_where itm = (Int_where)itms.Get_at(i);
|
||||
if (itm.Match(v)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
public boolean Parse(byte[] src) {
|
||||
byte[][] lines = Bry_.Split(src, Byte_ascii.Comma);
|
||||
int lines_len = lines.length;
|
||||
for (int i = 0; i < lines_len; i++) {
|
||||
if (!Parse_line(lines[i])) {
|
||||
itms.Clear();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private boolean Parse_line(byte[] src) {
|
||||
int src_len = src.length;
|
||||
int val_bgn = -1;
|
||||
int pos = -1;
|
||||
for (int i = 0; i < src_len; i++) {
|
||||
byte b = src[i];
|
||||
switch (b) {
|
||||
case Byte_ascii.Num_0: case Byte_ascii.Num_1: case Byte_ascii.Num_2: case Byte_ascii.Num_3: case Byte_ascii.Num_4:
|
||||
case Byte_ascii.Num_5: case Byte_ascii.Num_6: case Byte_ascii.Num_7: case Byte_ascii.Num_8: case Byte_ascii.Num_9:
|
||||
if (pos == -1) pos = i;
|
||||
break;
|
||||
case Byte_ascii.Dash:
|
||||
val_bgn = Bry_.Xto_int_or(src, pos, i, -1); if (val_bgn == -1) return false;
|
||||
pos = -1;
|
||||
break;
|
||||
default: // invalid char;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int val_end = Bry_.Xto_int_or(src, pos, src_len, -1); if (val_end == -1) return false;
|
||||
if (val_bgn == -1)
|
||||
itms.Add(new Int_where_val(val_end));
|
||||
else
|
||||
itms.Add(new Int_where_rng(val_bgn, val_end));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
interface Int_where {
|
||||
boolean Match(int v);
|
||||
}
|
||||
class Int_where_val implements Int_where {
|
||||
public Int_where_val(int val) {this.val = val;} private int val;
|
||||
public boolean Match(int v) {return v == val;}
|
||||
}
|
||||
class Int_where_rng implements Int_where {
|
||||
public Int_where_rng(int bgn, int end) {this.bgn = bgn; this.end = end;} private int bgn, end;
|
||||
public boolean Match(int v) {return v >= bgn && v <= end;}
|
||||
}
|
||||
@@ -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.xowa.xtns.syntaxHighlight; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Int_rng_mgr_tst {
|
||||
@Before public void init() {fxt.Clear();} Int_rng_mgr_fxt fxt = new Int_rng_mgr_fxt();
|
||||
@Test public void Val() {fxt.Test_parse_y("2") .Test_match_y(2) .Test_match_n(1, 3);}
|
||||
@Test public void Rng() {fxt.Test_parse_y("2-5") .Test_match_y(2, 3, 4, 5) .Test_match_n(0, 1, 6);}
|
||||
@Test public void Many() {fxt.Test_parse_y("1,3-5,7,9-10") .Test_match_y(1, 3, 4, 5, 7, 9, 10) .Test_match_n(0, 2, 6, 8, 11);}
|
||||
}
|
||||
class Int_rng_mgr_fxt {
|
||||
public void Clear() {
|
||||
if (mgr == null)
|
||||
mgr = new Int_rng_mgr_base();
|
||||
mgr.Clear();
|
||||
} Int_rng_mgr_base mgr;
|
||||
public Int_rng_mgr_fxt Test_parse_y(String raw) {return Test_parse(raw, true);}
|
||||
public Int_rng_mgr_fxt Test_parse_n(String raw) {return Test_parse(raw, false);}
|
||||
public Int_rng_mgr_fxt Test_parse(String raw, boolean expd) {Tfds.Eq(expd, mgr.Parse(Bry_.new_a7(raw))); return this;}
|
||||
public Int_rng_mgr_fxt Test_match_y(int... v) {return Test_match(v, true);}
|
||||
public Int_rng_mgr_fxt Test_match_n(int... v) {return Test_match(v, false);}
|
||||
public Int_rng_mgr_fxt Test_match(int[] ary, boolean expd) {
|
||||
int len = ary.length;
|
||||
for (int i = 0; i < len; i++) {
|
||||
Tfds.Eq(expd, mgr.Match(ary[i]), Int_.Xto_str(ary[i]));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
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.syntaxHighlight; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import gplx.core.primitives.*;
|
||||
import gplx.html.*; import gplx.xowa.html.*;
|
||||
public class Xtn_syntaxHighlight_nde implements Xox_xnde, Xop_xnde_atr_parser {
|
||||
private byte[] lang = Bry_.Empty; private byte[] style = null; private byte[] enclose = Bry_.Empty;
|
||||
private boolean line_enabled = false; private int start = 1; private Int_rng_mgr highlight_idxs = Int_rng_mgr_null._;
|
||||
public Xop_xnde_tkn Xnde() {throw Exc_.new_unimplemented();}
|
||||
public void Xatr_parse(Xowe_wiki wiki, byte[] src, Xop_xatr_itm xatr, Object xatr_obj) {
|
||||
if (xatr_obj == null) return;
|
||||
byte xatr_tid = ((Byte_obj_val)xatr_obj).Val();
|
||||
switch (xatr_tid) {
|
||||
case Xatr_enclose: enclose = xatr.Val_as_bry(src); break;
|
||||
case Xatr_lang: lang = xatr.Val_as_bry(src); break;
|
||||
case Xatr_style: style = xatr.Val_as_bry(src); break;
|
||||
case Xatr_line: line_enabled = true; break;
|
||||
case Xatr_start: start = xatr.Val_as_int_or(src, 1); break;
|
||||
case Xatr_highlight: highlight_idxs = new Int_rng_mgr_base(); highlight_idxs.Parse(xatr.Val_as_bry(src)); break;
|
||||
}
|
||||
}
|
||||
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
|
||||
Xoae_app app = ctx.App(); Xop_xnde_tag tag = xnde.Tag();
|
||||
ctx.Para().Process_block__xnde(tag, tag.Block_open()); // deactivate pre; pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-24
|
||||
Xop_xatr_itm.Xatr_parse(app, this, xatrs_syntaxHighlight, wiki, src, xnde);
|
||||
ctx.Para().Process_block__xnde(tag, tag.Block_close()); // deactivate pre; pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-24
|
||||
}
|
||||
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) {
|
||||
boolean lang_is_text = Bry_.Eq(lang, Lang_text);
|
||||
boolean enclose_is_none = Bry_.Eq(enclose, Enclose_none);
|
||||
if (enclose_is_none) {
|
||||
bfr.Add(Xoh_consts.Span_bgn);
|
||||
}
|
||||
else if (lang_is_text) {
|
||||
bfr.Add(Xoh_consts.Code_bgn_open);
|
||||
if (style != null)
|
||||
bfr.Add(Xoh_consts.Style_atr).Add(this.style).Add_byte(Byte_ascii.Quote);
|
||||
bfr.Add_byte(Byte_ascii.Gt);
|
||||
}
|
||||
else {
|
||||
bfr.Add(Xoh_consts.Pre_bgn_open);
|
||||
bfr.Add(Xoh_consts.Style_atr).Add(Xoh_consts.Pre_style_overflow_auto);
|
||||
if (style != null) bfr.Add(style);
|
||||
bfr.Add(Xoh_consts.__end_quote);
|
||||
}
|
||||
int text_bgn = xnde.Tag_open_end();
|
||||
int text_end = Bry_finder.Find_bwd_while(src, xnde.Tag_close_bgn(), -1, Byte_ascii.Space) + 1; // trim space from end; PAGE:en.w:Comment_(computer_programming) DATE:2014-06-23
|
||||
if (line_enabled || enclose_is_none) {
|
||||
bfr.Add_byte_nl();
|
||||
byte[][] lines = Bry_.Split_lines(Bry_.Mid(src, text_bgn, text_end));
|
||||
int lines_len = lines.length;
|
||||
int line_idx = start;
|
||||
int line_end = (line_idx + lines_len) - 1; // EX: line_idx=9 line_len=1; line_end=9
|
||||
int digits_max = Int_.DigitCount(line_end);
|
||||
for (int i = 0; i < lines_len; i++) {
|
||||
byte[] line = lines[i]; if (i == 0 && Bry_.Len_eq_0(line)) continue;
|
||||
if (line_enabled) {
|
||||
bfr.Add(Xoh_consts.Span_bgn_open).Add(Xoh_consts.Style_atr).Add(Style_line).Add(Xoh_consts.__end_quote);
|
||||
int pad = digits_max - Int_.DigitCount(line_idx);
|
||||
if (pad > 0) bfr.Add_byte_repeat(Byte_ascii.Space, pad);
|
||||
bfr.Add_int_variable(line_idx++).Add_byte(Byte_ascii.Space);
|
||||
bfr.Add(Xoh_consts.Span_end);
|
||||
}
|
||||
bfr.Add(Xoh_consts.Span_bgn_open);
|
||||
if (highlight_idxs.Match(i))
|
||||
bfr.Add(Xoh_consts.Style_atr).Add(Style_highlight).Add(Xoh_consts.__end_quote);
|
||||
else
|
||||
bfr.Add(Xoh_consts.__end);
|
||||
Xox_mgr_base.Xtn_write_escape(app, bfr, line);
|
||||
bfr.Add(Xoh_consts.Span_end);
|
||||
if (enclose_is_none)
|
||||
bfr.Add(Html_tag_.Br_inl);
|
||||
bfr.Add_byte_nl();
|
||||
}
|
||||
}
|
||||
else
|
||||
Xox_mgr_base.Xtn_write_escape(app, bfr, src, text_bgn, text_end);
|
||||
if (enclose_is_none) bfr.Add(Xoh_consts.Span_end);
|
||||
else if (lang_is_text) bfr.Add(Xoh_consts.Code_end);
|
||||
else bfr.Add(Xoh_consts.Pre_end);
|
||||
}
|
||||
private static final byte[] Lang_text = Bry_.new_a7("text"), Style_line = Bry_.new_a7("-moz-user-select:none;"), Style_highlight = Bry_.new_a7("background-color: #FFFFCC;"), Enclose_none = Bry_.new_a7("none");
|
||||
public static final byte Xatr_enclose = 2, Xatr_lang = 3, Xatr_style = 4, Xatr_line = 5, Xatr_start = 6, Xatr_highlight = 7;
|
||||
private static final Hash_adp_bry xatrs_syntaxHighlight = Hash_adp_bry.ci_ascii_().Add_str_byte("enclose", Xatr_enclose).Add_str_byte("lang", Xatr_lang).Add_str_byte("style", Xatr_style).Add_str_byte("line", Xatr_line).Add_str_byte("start", Xatr_start).Add_str_byte("highlight", Xatr_highlight);
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
/*
|
||||
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.syntaxHighlight; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
|
||||
import org.junit.*;
|
||||
public class Xtn_syntaxHighlight_nde_tst {
|
||||
private Xop_fxt fxt = new Xop_fxt();
|
||||
@Test public void Basic() {
|
||||
fxt.Test_parse_page_all_str("<syntaxHighlight>abc</syntaxHighlight>", "<pre style=\"overflow:auto;\">abc</pre>");
|
||||
}
|
||||
@Test public void Text() {
|
||||
fxt.Test_parse_page_all_str("<syntaxHighlight lang=\"text\">abc</syntaxHighlight>", "<code>abc</code>");
|
||||
}
|
||||
@Test public void Style_pre() {
|
||||
fxt.Test_parse_page_all_str("<syntaxHighlight style=\"color:red;\">abc</syntaxHighlight>", "<pre style=\"overflow:auto;color:red;\">abc</pre>");
|
||||
}
|
||||
@Test public void Style_code() {
|
||||
fxt.Test_parse_page_all_str("<syntaxHighlight lang=\"text\" style=\"color:red;\">abc</syntaxHighlight>", "<code style=\"color:red;\">abc</code>");
|
||||
}
|
||||
@Test public void Trim_ws() {
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight>"
|
||||
, "abc"
|
||||
, "</syntaxHighlight>"
|
||||
), String_.Concat_lines_nl
|
||||
( "<pre style=\"overflow:auto;\">"
|
||||
, "abc"
|
||||
, "</pre>"
|
||||
));
|
||||
}
|
||||
@Test public void Line() {
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight line>"
|
||||
, "a"
|
||||
, "b"
|
||||
, "</syntaxHighlight>"
|
||||
), String_.Concat_lines_nl
|
||||
( "<pre style=\"overflow:auto;\">"
|
||||
, "<span style=\"-moz-user-select:none;\">1 </span><span>a</span>"
|
||||
, "<span style=\"-moz-user-select:none;\">2 </span><span>b</span>"
|
||||
, "</pre>"
|
||||
));
|
||||
}
|
||||
@Test public void Start() {
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight line start=3>"
|
||||
, "a"
|
||||
, "b"
|
||||
, "</syntaxHighlight>"
|
||||
), String_.Concat_lines_nl
|
||||
( "<pre style=\"overflow:auto;\">"
|
||||
, "<span style=\"-moz-user-select:none;\">3 </span><span>a</span>"
|
||||
, "<span style=\"-moz-user-select:none;\">4 </span><span>b</span>"
|
||||
, "</pre>"
|
||||
));
|
||||
}
|
||||
@Test public void Highlight() {
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight line highlight='1,3'>"
|
||||
, "a"
|
||||
, "b"
|
||||
, "c"
|
||||
, "</syntaxHighlight>"
|
||||
), String_.Concat_lines_nl
|
||||
( "<pre style=\"overflow:auto;\">"
|
||||
, "<span style=\"-moz-user-select:none;\">1 </span><span style=\"background-color: #FFFFCC;\">a</span>"
|
||||
, "<span style=\"-moz-user-select:none;\">2 </span><span>b</span>"
|
||||
, "<span style=\"-moz-user-select:none;\">3 </span><span style=\"background-color: #FFFFCC;\">c</span>"
|
||||
, "</pre>"
|
||||
));
|
||||
}
|
||||
@Test public void Enclose_none() {
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight enclose=none>"
|
||||
, "a"
|
||||
, "b"
|
||||
, "c"
|
||||
, "</syntaxHighlight>"
|
||||
), String_.Concat_lines_nl
|
||||
( "<span>"
|
||||
, "<span>a</span><br/>"
|
||||
, "<span>b</span><br/>"
|
||||
, "<span>c</span><br/>"
|
||||
, "</span>"
|
||||
));
|
||||
}
|
||||
@Test public void Line_padded() {
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight line start=9>"
|
||||
, "a"
|
||||
, "b"
|
||||
, "</syntaxHighlight>"
|
||||
), String_.Concat_lines_nl
|
||||
( "<pre style=\"overflow:auto;\">"
|
||||
, "<span style=\"-moz-user-select:none;\"> 9 </span><span>a</span>"
|
||||
, "<span style=\"-moz-user-select:none;\">10 </span><span>b</span>"
|
||||
, "</pre>"
|
||||
));
|
||||
}
|
||||
@Test public void EndTag_has_ws() { // PURPOSE: </syntaxhighlight > not being closed correctly; PAGE:en.w:Mergesort; updated; DATE:2014-06-24
|
||||
fxt.Init_para_y_();
|
||||
fxt.Test_parse_page_wiki_str(String_.Concat_lines_nl_skip_last
|
||||
( "a"
|
||||
, "<syntaxhighlight>"
|
||||
, "b"
|
||||
, "</syntaxhighlight >"
|
||||
, "c"
|
||||
, "<syntaxhighlight>"
|
||||
, "d"
|
||||
, "</syntaxhighlight>"
|
||||
), String_.Concat_lines_nl_skip_last
|
||||
( "<p>a"
|
||||
, "</p>"
|
||||
, "<pre style=\"overflow:auto;\">"
|
||||
, "b"
|
||||
, "</pre>"
|
||||
, ""
|
||||
, "<p>c"
|
||||
, "</p>"
|
||||
, "<pre style=\"overflow:auto;\">"
|
||||
, "d"
|
||||
, "</pre>"
|
||||
, ""
|
||||
));
|
||||
fxt.Init_para_n_();
|
||||
}
|
||||
@Test public void Trim_ws_from_end_tab() {// PURPOSE: trim ws between "abc" and "</syntaxhighlight"; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-23
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "<syntaxHighlight>"
|
||||
, "abc"
|
||||
, " </syntaxHighlight>" // trim ws here
|
||||
), String_.Concat_lines_nl
|
||||
( "<pre style=\"overflow:auto;\">"
|
||||
, "abc"
|
||||
, "</pre>"
|
||||
));
|
||||
}
|
||||
@Test public void Pre() {// PURPOSE: handle pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-23
|
||||
fxt.Init_para_y_();
|
||||
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
|
||||
( "a"
|
||||
, ""
|
||||
, " <syntaxHighlight>"
|
||||
, " b"
|
||||
, " </syntaxHighlight>" // trim ws here
|
||||
, ""
|
||||
, "c"
|
||||
), String_.Concat_lines_nl
|
||||
( "<p>a"
|
||||
, "</p>"
|
||||
, " <pre style=\"overflow:auto;\">"
|
||||
, " b"
|
||||
, "</pre>"
|
||||
, ""
|
||||
, "<p>c"
|
||||
, "</p>"
|
||||
));
|
||||
fxt.Init_para_n_();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user