1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2025-05-31 22:44:34 +00:00

SyntaxHighlight: Treat syntaxHighlight inline as enclose=none [#794]

This commit is contained in:
gnosygnu 2020-08-27 05:25:59 -04:00
parent 4f87d1a195
commit 1eea3682ca
2 changed files with 263 additions and 214 deletions

View File

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, 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. or alternatively under the terms of the Apache License Version 2.0.
@ -13,43 +13,65 @@ 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 GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.syntax_highlights; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; package gplx.xowa.xtns.syntax_highlights;
import gplx.core.primitives.*;
import gplx.xowa.parsers.*; import gplx.xowa.parsers.xndes.*; import gplx.xowa.parsers.htmls.*; import gplx.Bry_;
import gplx.xowa.htmls.*; import gplx.xowa.htmls.core.htmls.*; import gplx.Bry_bfr;
public class Synh_xtn_nde implements Xox_xnde, Mwh_atr_itm_owner1 { import gplx.Err_;
private byte[] lang = Bry_.Empty; private byte[] style = null; private byte[] enclose = Bry_.Empty; import gplx.Hash_adp_bry;
private boolean line_enabled = false; private int start = 1; private Int_rng_mgr highlight_idxs = Int_rng_mgr_null.Instance; import gplx.core.primitives.Byte_obj_val;
public Xop_xnde_tkn Xnde() {throw Err_.new_unimplemented();} import gplx.xowa.Xoae_app;
public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, Object xatr_id_obj) { import gplx.xowa.Xoae_page;
if (xatr_id_obj == null) return; import gplx.xowa.Xowe_wiki;
Byte_obj_val xatr_id = (Byte_obj_val)xatr_id_obj; import gplx.xowa.htmls.core.htmls.Xoh_html_wtr;
switch (xatr_id.Val()) { import gplx.xowa.htmls.core.htmls.Xoh_wtr_ctx;
case Xatr_line: line_enabled = true; break; import gplx.xowa.parsers.Xop_ctx;
case Xatr_enclose: enclose = xatr.Val_as_bry(); break; import gplx.xowa.parsers.Xop_root_tkn;
case Xatr_lang: lang = xatr.Val_as_bry(); break; import gplx.xowa.parsers.htmls.Mwh_atr_itm;
case Xatr_style: style = xatr.Val_as_bry(); break; import gplx.xowa.parsers.htmls.Mwh_atr_itm_owner1;
case Xatr_start: start = xatr.Val_as_int_or(1); break; import gplx.xowa.parsers.xndes.Xop_xnde_tag;
case Xatr_highlight: highlight_idxs = new Int_rng_mgr_base(); highlight_idxs.Parse(xatr.Val_as_bry()); break; import gplx.xowa.parsers.xndes.Xop_xnde_tkn;
} import gplx.xowa.xtns.Xox_xnde;
} import gplx.xowa.xtns.Xox_xnde_;
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(); public class Synh_xtn_nde implements Xox_xnde, Mwh_atr_itm_owner1 {
ctx.Para().Process_block__xnde(tag, tag.Block_open()); // deactivate pre; pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-24 private byte[] lang = Bry_.Empty;
Xox_xnde_.Xatr__set(wiki, this, xatrs_hash, src, xnde); private byte[] style = null;
ctx.Para().Process_block__xnde(tag, tag.Block_close()); // deactivate pre; pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-24 private byte[] enclose = Bry_.Empty;
} private boolean line_enabled = false;
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) { private int start = 1;
Synh_xtn_nde_.Make(bfr, app, src, xnde.Tag_open_end(), xnde.Tag_close_bgn(), lang, enclose, style, line_enabled, start, highlight_idxs); private Int_rng_mgr highlight_idxs = Int_rng_mgr_null.Instance;
} public Xop_xnde_tkn Xnde() {throw Err_.new_unimplemented();}
private static final byte Xatr_enclose = 1, Xatr_lang = 2, Xatr_style = 3, Xatr_line = 4, Xatr_start = 5, Xatr_highlight = 6; public void Xatr__set(Xowe_wiki wiki, byte[] src, Mwh_atr_itm xatr, Object xatr_id_obj) {
private static final Hash_adp_bry xatrs_hash = Hash_adp_bry.ci_a7() if (xatr_id_obj == null) return;
.Add_str_byte("enclose" , Xatr_enclose) Byte_obj_val xatr_id = (Byte_obj_val)xatr_id_obj;
.Add_str_byte("inline" , Xatr_enclose) switch (xatr_id.Val()) {
.Add_str_byte("lang" , Xatr_lang) case Xatr_line: line_enabled = true; break;
.Add_str_byte("style" , Xatr_style) case Xatr_enclose: enclose = xatr.Val_as_bry(); break;
.Add_str_byte("line" , Xatr_line) case Xatr_inline: enclose = Bry_.new_a7("none"); break; // 2020-08-27|ISSUE#:794|inline should be enclose=none
.Add_str_byte("start" , Xatr_start) case Xatr_lang: lang = xatr.Val_as_bry(); break;
.Add_str_byte("highlight" , Xatr_highlight) 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, Xatr_inline = 7;
private static final Hash_adp_bry xatrs_hash = Hash_adp_bry.ci_a7()
.Add_str_byte("enclose" , Xatr_enclose)
.Add_str_byte("inline" , Xatr_inline)
.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

@ -1,6 +1,6 @@
/* /*
XOWA: the XOWA Offline Wiki Application XOWA: the XOWA Offline Wiki Application
Copyright (C) 2012-2017 gnosygnu@gmail.com Copyright (C) 2012-2020 gnosygnu@gmail.com
XOWA is licensed under the terms of the General Public License (GPL) Version 3, 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. or alternatively under the terms of the Apache License Version 2.0.
@ -13,175 +13,202 @@ 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 GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
*/ */
package gplx.xowa.xtns.syntax_highlights; import gplx.*; import gplx.xowa.*; import gplx.xowa.xtns.*; package gplx.xowa.xtns.syntax_highlights;
import org.junit.*;
public class Synh_xtn_nde_tst { import gplx.String_;
private final Xop_fxt fxt = new Xop_fxt(); import gplx.xowa.Xop_fxt;
@Test public void Basic() { import org.junit.Test;
fxt.Test_parse_page_all_str("<syntaxHighlight>abc</syntaxHighlight>", "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">abc</pre></div>");
} public class Synh_xtn_nde_tst {
@Test public void Text() { private final Xop_fxt fxt = new Xop_fxt();
fxt.Test_parse_page_all_str("<syntaxHighlight lang=\"text\">abc</syntaxHighlight>", "<div class=\"mw-highlight\"><pre style=\"overflow:auto\" class=\"prettyprint lang-text\">abc</pre></div>"); @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 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 Text() {
} fxt.Test_parse_page_all_str("<syntaxHighlight lang=\"text\">abc</syntaxHighlight>", "<div class=\"mw-highlight\"><pre style=\"overflow:auto\" class=\"prettyprint lang-text\">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\" class=\"prettyprint lang-text\">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 Trim_ws() { }
fxt.Test_parse_page_all_str(String_.Concat_lines_nl @Test public void Style_code() {
( "<syntaxHighlight>" 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\" class=\"prettyprint lang-text\">abc</pre></div>");
, "abc" }
, "</syntaxHighlight>" @Test public void Trim_ws() {
), String_.Concat_lines_nl fxt.Test_parse_page_all_str(String_.Concat_lines_nl
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" ( "<syntaxHighlight>"
, "abc" , "abc"
, "</pre></div>" , "</syntaxHighlight>"
)); ), String_.Concat_lines_nl
} ( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
@Test public void Line() { , "abc"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl , "</pre></div>"
( "<syntaxHighlight line>" ));
, "a" }
, "b" @Test public void Line() {
, "</syntaxHighlight>" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
), String_.Concat_lines_nl ( "<syntaxHighlight line>"
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "a"
, "<span style=\"-moz-user-select:none;\">1 </span><span>a</span>" , "b"
, "<span style=\"-moz-user-select:none;\">2 </span><span>b</span>" , "</syntaxHighlight>"
, "</pre></div>" ), String_.Concat_lines_nl
)); ( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
} , "<span style=\"-moz-user-select:none;\">1 </span><span>a</span>"
@Test public void Start() { , "<span style=\"-moz-user-select:none;\">2 </span><span>b</span>"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl , "</pre></div>"
( "<syntaxHighlight line start=3>" ));
, "a" }
, "b" @Test public void Start() {
, "</syntaxHighlight>" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
), String_.Concat_lines_nl ( "<syntaxHighlight line start=3>"
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "a"
, "<span style=\"-moz-user-select:none;\">3 </span><span>a</span>" , "b"
, "<span style=\"-moz-user-select:none;\">4 </span><span>b</span>" , "</syntaxHighlight>"
, "</pre></div>" ), String_.Concat_lines_nl
)); ( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
} , "<span style=\"-moz-user-select:none;\">3 </span><span>a</span>"
@Test public void Highlight() { , "<span style=\"-moz-user-select:none;\">4 </span><span>b</span>"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl , "</pre></div>"
( "<syntaxHighlight line highlight='1,3'>" ));
, "a" }
, "b" @Test public void Highlight() {
, "c" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
, "</syntaxHighlight>" ( "<syntaxHighlight line highlight='1,3'>"
), String_.Concat_lines_nl , "a"
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "b"
, "<span style=\"-moz-user-select:none;\">1 </span><span style=\"background-color: #FFFFCC;\">a</span>" , "c"
, "<span style=\"-moz-user-select:none;\">2 </span><span>b</span>" , "</syntaxHighlight>"
, "<span style=\"-moz-user-select:none;\">3 </span><span style=\"background-color: #FFFFCC;\">c</span>" ), String_.Concat_lines_nl
, "</pre></div>" ( "<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>"
@Test public void Highlight_wo_line_arg() { , "<span style=\"-moz-user-select:none;\">3 </span><span style=\"background-color: #FFFFCC;\">c</span>"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl , "</pre></div>"
( "<syntaxHighlight highlight='1,3'>" ));
, "a" }
, "b" @Test public void Highlight_wo_line_arg() {
, "c" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
, "</syntaxHighlight>" ( "<syntaxHighlight highlight='1,3'>"
), String_.Concat_lines_nl , "a"
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "b"
, "<span style=\"background-color: #FFFFCC;\">a</span>" , "c"
, "<span>b</span>" , "</syntaxHighlight>"
, "<span style=\"background-color: #FFFFCC;\">c</span>" ), String_.Concat_lines_nl
, "</pre></div>" ( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
)); , "<span style=\"background-color: #FFFFCC;\">a</span>"
} , "<span>b</span>"
@Test public void Enclose_none() { , "<span style=\"background-color: #FFFFCC;\">c</span>"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl , "</pre></div>"
( "<syntaxHighlight enclose=none style='color:red'>" ));
, "a" }
, "b" @Test public void Enclose_none() {
, "c" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
, "</syntaxHighlight>" ( "<syntaxHighlight enclose=none style='color:red'>"
), String_.Concat_lines_nl , "a"
( "<code class=\"mw-highlight\" style=\"color:red\">" , "b"
, "a" , "c"
, "b" , "</syntaxHighlight>"
, "c" ), String_.Concat_lines_nl
, "</code>" ( "<code class=\"mw-highlight\" style=\"color:red\">"
)); , "a"
} , "b"
@Test public void Line_padded() { , "c"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl , "</code>"
( "<syntaxHighlight line start=9>" ));
, "a" }
, "b" @Test public void Line_padded() {
, "</syntaxHighlight>" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
), String_.Concat_lines_nl ( "<syntaxHighlight line start=9>"
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "a"
, "<span style=\"-moz-user-select:none;\"> 9 </span><span>a</span>" , "b"
, "<span style=\"-moz-user-select:none;\">10 </span><span>b</span>" , "</syntaxHighlight>"
, "</pre></div>" ), String_.Concat_lines_nl
)); ( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
} , "<span style=\"-moz-user-select:none;\"> 9 </span><span>a</span>"
@Test public void EndTag_has_ws() { // PURPOSE: </syntaxhighlight > not being closed correctly; PAGE:en.w:Mergesort; updated; DATE:2014-06-24 , "<span style=\"-moz-user-select:none;\">10 </span><span>b</span>"
fxt.Init_para_y_(); , "</pre></div>"
fxt.Test_parse_page_wiki_str(String_.Concat_lines_nl_skip_last ));
( "a" }
, "<syntaxhighlight>" @Test public void EndTag_has_ws() { // PURPOSE: </syntaxhighlight > not being closed correctly; PAGE:en.w:Mergesort; updated; DATE:2014-06-24
, "b" fxt.Init_para_y_();
, "</syntaxhighlight >" fxt.Test_parse_page_wiki_str(String_.Concat_lines_nl_skip_last
, "c" ( "a"
, "<syntaxhighlight>" , "<syntaxhighlight>"
, "d" , "b"
, "</syntaxhighlight>" , "</syntaxhighlight >"
), String_.Concat_lines_nl_skip_last , "c"
( "<p>a" , "<syntaxhighlight>"
, "</p>" , "d"
, "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "</syntaxhighlight>"
, "b" ), String_.Concat_lines_nl_skip_last
, "</pre></div>" ( "<p>a"
, "" , "</p>"
, "<p>c" , "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, "</p>" , "b"
, "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "</pre></div>"
, "d" , ""
, "</pre></div>" , "<p>c"
, "" , "</p>"
)); , "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
fxt.Init_para_n_(); , "d"
} , "</pre></div>"
@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>" fxt.Init_para_n_();
, "abc" }
, " </syntaxHighlight>" // trim ws here @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
), String_.Concat_lines_nl fxt.Test_parse_page_all_str(String_.Concat_lines_nl
( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">" ( "<syntaxHighlight>"
, "abc" , "abc"
, "</pre></div>" , " </syntaxHighlight>" // trim ws here
)); ), String_.Concat_lines_nl
} ( "<div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
@Test public void Pre() {// PURPOSE: handle pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-23 , "abc"
fxt.Init_para_y_(); , "</pre></div>"
fxt.Test_parse_page_all_str(String_.Concat_lines_nl ));
( "a" }
, "" @Test public void Pre() {// PURPOSE: handle pre; PAGE:en.w:Comment_(computer_programming); DATE:2014-06-23
, " <syntaxHighlight>" fxt.Init_para_y_();
, " b" fxt.Test_parse_page_all_str(String_.Concat_lines_nl
, " </syntaxHighlight>" // trim ws here ( "a"
, "" , ""
, "c" , " <syntaxHighlight>"
), String_.Concat_lines_nl , " b"
( "<p>a" , " </syntaxHighlight>" // trim ws here
, "</p>" , ""
, " <div class=\"mw-highlight\"><pre style=\"overflow:auto\">" , "c"
, " b" ), String_.Concat_lines_nl
, "</pre></div>" ( "<p>a"
, "" , "</p>"
, "<p>c" , " <div class=\"mw-highlight\"><pre style=\"overflow:auto\">"
, "</p>" , " b"
)); , "</pre></div>"
fxt.Init_para_n_(); , ""
} , "<p>c"
} , "</p>"
));
fxt.Init_para_n_();
}
@Test public void Inline() {
// 2020-08-27|ISSUE#:794|inline should be enclose=none
fxt.Init_para_y_();
fxt.Test_parse_page_all_str(String_.Concat_lines_nl
( "a"
, ""
, " <syntaxHighlight inline>"
, " b"
, " </syntaxHighlight>" // trim ws here
, ""
, "c"
), String_.Concat_lines_nl
( "<p>a"
, "</p>"
, " <code class=\"mw-highlight\">" // fails with <div class="mw-highlight"><pre style="overflow:auto">
, " b"
, "</code>"
, ""
, "<p>c"
, "</p>"
));
fxt.Init_para_n_();
}
}