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

Source: Restore broken commit

This commit is contained in:
gnosygnu
2017-02-06 22:14:55 -05:00
parent 938beac9f9
commit 3bfeb94b43
4380 changed files with 328018 additions and 0 deletions

View 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.syntax_highlights; 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 Instance = 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_.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_.To_int_or(src, pos, i, -1); if (val_bgn == -1) return false;
pos = -1;
break;
default: // invalid char;
return false;
}
}
int val_end = Bry_.To_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;}
}

View File

@@ -0,0 +1,46 @@
/*
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.syntax_highlights; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Int_rng_mgr_tst {
private final Int_rng_mgr_fxt fxt = new Int_rng_mgr_fxt();
@Before public void init() {fxt.Clear();}
@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 {
private Int_rng_mgr_base mgr;
public void Clear() {
if (mgr == null)
mgr = new Int_rng_mgr_base();
mgr.Clear();
}
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_.To_str(ary[i]));
}
return this;
}
}

View File

@@ -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.xtns.syntax_highlights; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.core.primitives.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.htmls.*;
import gplx.xowa.htmls.*; import gplx.xowa.htmls.core.htmls.*;
public class Synh_xtn_nde implements Xox_xnde, Mwh_atr_itm_owner1 {
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.Instance;
public Xop_xnde_tkn Xnde() {throw Err_.new_unimplemented();}
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, Object xatr_id_obj) {
if (xatr_id_obj == null) return;
Byte_obj_val xatr_id = (Byte_obj_val)xatr_id_obj;
switch (xatr_id.Val()) {
case Xatr_line: line_enabled = true; break;
case Xatr_enclose: enclose = xatr.Val_as_bry(); break;
case Xatr_lang: lang = xatr.Val_as_bry(); break;
case Xatr_style: style = xatr.Val_as_bry(); break;
case Xatr_start: start = xatr.Val_as_int_or(1); break;
case Xatr_highlight: highlight_idxs = new Int_rng_mgr_base(); highlight_idxs.Parse(xatr.Val_as_bry()); break;
}
}
public void Xtn_parse(Xowe_wiki wiki, Xop_ctx ctx, Xop_root_tkn root, byte[] src, Xop_xnde_tkn xnde) {
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
Xox_xnde_.Xatr__set(wiki, this, xatrs_hash, 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, Xoae_page wpg, Xop_xnde_tkn xnde, byte[] src) {
Synh_xtn_nde_.Make(bfr, app, src, xnde.Tag_open_end(), xnde.Tag_close_bgn(), lang, enclose, style, line_enabled, start, highlight_idxs);
}
private static final byte Xatr_enclose = 1, Xatr_lang = 2, Xatr_style = 3, Xatr_line = 4, Xatr_start = 5, Xatr_highlight = 6;
private static final Hash_adp_bry xatrs_hash = Hash_adp_bry.ci_a7()
.Add_str_byte("enclose" , Xatr_enclose)
.Add_str_byte("inline" , 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)
;
}

View File

@@ -0,0 +1,78 @@
/*
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.syntax_highlights; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import gplx.langs.htmls.*; import gplx.xowa.htmls.*;
class Synh_xtn_nde_ {
public static void Make(Bry_bfr bfr, Xoae_app app, byte[] src, int src_bgn, int src_end, byte[] lang, byte[] enclose, byte[] style, boolean line_enabled, int start, Int_rng_mgr highlight_idxs) {
boolean enclose_is_none = Bry_.Eq(enclose, Enclose_none);
if (enclose_is_none) { // enclose=none -> put in <code>
bfr.Add(Bry__code_bgn);
if (style != null) bfr.Add(Xoh_consts.Style_atr).Add(style).Add_byte(Byte_ascii.Quote);
bfr.Add_byte(Byte_ascii.Gt);
}
else {
bfr.Add(Bry__div_bgn);
if (style != null) bfr.Add(Xoh_consts.Style_atr).Add(style).Add_byte(Byte_ascii.Quote);
bfr.Add_byte(Byte_ascii.Angle_end);
bfr.Add(Xoh_consts.Pre_bgn_overflow);
}
int text_bgn = src_bgn;
int text_end = Bry_find_.Find_bwd_while(src, src_end, -1, Byte_ascii.Space) + 1; // trim space from end; PAGE:en.w:Comment_(computer_programming) DATE:2014-06-23
if (line_enabled) {
bfr.Add_byte_nl();
byte[][] lines = Bry_split_.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);
bfr.Add_byte_nl();
}
}
else
Xox_mgr_base.Xtn_write_escape_pre(app, bfr, src, text_bgn, text_end);
if (enclose_is_none) {
bfr.Add(Xoh_consts.Code_end);
}
else {
bfr.Add(Xoh_consts.Pre_end);
bfr.Add(Gfh_bldr_.Bry__div_rhs);
}
}
private static final byte[]
Enclose_none = Bry_.new_a7("none")
, Style_line = Bry_.new_a7("-moz-user-select:none;"), Style_highlight = Bry_.new_a7("background-color: #FFFFCC;")
, Bry__div_bgn = Bry_.new_a7("<div class=\"mw-highlight\"")
, Bry__code_bgn = Bry_.new_a7("<code class=\"mw-highlight\"")
;
}

View File

@@ -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.syntax_highlights; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*;
import org.junit.*;
public class Synh_xtn_nde_tst {
private final Xop_fxt fxt = new Xop_fxt();
@Test public void Basic() {
fxt.Test_parse_page_all_str("<syntaxHighlight>abc</syntaxHighlight>", "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">abc</pre></div>");
}
@Test public void Text() {
fxt.Test_parse_page_all_str("<syntaxHighlight lang=\"text\">abc</syntaxHighlight>", "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">abc</pre></div>");
}
@Test public void Style_pre() {
fxt.Test_parse_page_all_str("<syntaxHighlight style=\"color:red;\">abc</syntaxHighlight>", "<div class=\"mw-highlight\" style=\"color:red;\"><pre style=\"overflow:auto\">abc</pre></div>");
}
@Test public void Style_code() {
fxt.Test_parse_page_all_str("<syntaxHighlight lang=\"text\" style=\"color:red;\">abc</syntaxHighlight>", "<div class=\"mw-highlight\" style=\"color:red;\"><pre style=\"overflow:auto\">abc</pre></div>");
}
@Test public void Trim_ws() {
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
( "<syntaxHighlight>"
, "abc"
, "</syntaxHighlight>"
), String_.Concat_lines_nl
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, "abc"
, "</pre></div>"
));
}
@Test public void Line() {
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
( "<syntaxHighlight line>"
, "a"
, "b"
, "</syntaxHighlight>"
), String_.Concat_lines_nl
( "<div class=\"mw-highlight\"><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></div>"
));
}
@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
( "<div class=\"mw-highlight\"><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></div>"
));
}
@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
( "<div class=\"mw-highlight\"><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></div>"
));
}
@Test public void Enclose_none() {
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
( "<syntaxHighlight enclose=none style='color:red'>"
, "a"
, "b"
, "c"
, "</syntaxHighlight>"
), String_.Concat_lines_nl
( "<code class=\"mw-highlight\" style=\"color:red\">"
, "a"
, "b"
, "c"
, "</code>"
));
}
@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
( "<div class=\"mw-highlight\"><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></div>"
));
}
@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>"
, "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, "b"
, "</pre></div>"
, ""
, "<p>c"
, "</p>"
, "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, "d"
, "</pre></div>"
, ""
));
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
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, "abc"
, "</pre></div>"
));
}
@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>"
, " <div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, " b"
, "</pre></div>"
, ""
, "<p>c"
, "</p>"
));
fxt.Init_para_n_();
}
}