GfmlStringHighlighter.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012 gnosygnu@gmail.com
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as
  6. published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package gplx.gfml; import gplx.*;
  16. import gplx.core.strings.*;
  17. class GfmlStringHighlighter {
  18. public String Raw() {return raw;} public GfmlStringHighlighter Raw_(String v) {raw = v; return this;} private String raw;
  19. public int ExcerptLen() {return excerptLen;} public GfmlStringHighlighter ExcerptLen_(int v) {excerptLen = v; return this;} int excerptLen = 40;
  20. public GfmlStringHighlighter Mark_(int pos, char c, String msg) {
  21. marks.Add(new GfmlStringHighlighterMarker().Pos_(pos).Sym_(c).Msg_(msg));
  22. return this;
  23. }
  24. int XtoBgnPos(int pos, int prvEndPos) {
  25. int rv = pos - excerptLen;
  26. if (rv < prvEndPos) rv = prvEndPos; // ensure bgnPos is not < prev end pos; ex: marks of 5,12; at 12, bgnPos = 2 which is less than 5; make 5
  27. return rv;
  28. }
  29. public String[] Gen() {
  30. String_bldr posBfr = String_bldr_.new_(), rawBfr = String_bldr_.new_(), symBfr = String_bldr_.new_();
  31. List_adp symList = List_adp_.New();
  32. int bgnPos = 0, endPos = 0;
  33. int rawLen = String_.Len(raw); int rawLenDigits = Int_.DigitCount(rawLen);
  34. int rawBfrBgn = -1, marksLastIdx = marks.Idx_last();
  35. for (int i = 0; i < marks.Count(); i++) {
  36. GfmlStringHighlighterMarker curMark = (GfmlStringHighlighterMarker)marks.Get_at(i);
  37. GfmlStringHighlighterMarker nxtMark = i == marksLastIdx ? GfmlStringHighlighterMarker.Null : (GfmlStringHighlighterMarker)marks.Get_at(i + 1);
  38. // bgnPos
  39. bgnPos = XtoBgnPos(curMark.Pos(), endPos);
  40. if (i == 0) rawBfrBgn = bgnPos;
  41. // endPos
  42. int nxtMarkPos = nxtMark == GfmlStringHighlighterMarker.Null ? Int_.Max_value : nxtMark.Pos();
  43. endPos = curMark.Pos() + excerptLen;
  44. if (endPos >= nxtMarkPos) endPos = nxtMarkPos;
  45. if (endPos > rawLen ) endPos = rawLen + 1;
  46. // build bfrs
  47. for (int j = bgnPos; j < endPos; j++) {
  48. char rawChar = j == rawLen ? ' ' : String_.CharAt(raw, j);
  49. if (rawChar == '\t') {posBfr.Add("t"); rawBfr.Add(" ");}
  50. else if (rawChar == '\n') {posBfr.Add("n"); rawBfr.Add(" ");}
  51. else {
  52. char posChar = j == rawLen ? '>' : ' ';
  53. posBfr.Add(posChar);
  54. rawBfr.Add(rawChar);
  55. }
  56. char symChar = j == curMark.Pos() ? curMark.Sym() : ' ';
  57. symBfr.Add(symChar);
  58. }
  59. // gap
  60. int nxtMarkBgn = XtoBgnPos(nxtMark.Pos(), endPos);
  61. int gap = nxtMarkBgn - endPos;
  62. if (gap > 0) {
  63. int gapDigits = Int_.DigitCount(gap);
  64. posBfr.Add_fmt("[{0}]", Int_.To_str_pad_bgn_zero(gap, gapDigits));
  65. rawBfr.Add_fmt("[{0}]", String_.Repeat(".", gapDigits));
  66. symBfr.Add_fmt(" {0} ", String_.Repeat(" ", gapDigits));
  67. }
  68. if (curMark.Sym() != ' ')
  69. symList.Add(String_.Format("[{0}] {1} {2}", Int_.To_str_pad_bgn_zero(curMark.Pos(), rawLenDigits), curMark.Sym(), curMark.Msg()));
  70. }
  71. if (rawBfrBgn == 0) {
  72. posBfr.Add_at(0, "<");
  73. rawBfr.Add_at(0, " ");
  74. symBfr.Add_at(0, " ");
  75. }
  76. List_adp rv = List_adp_.New();
  77. rv.Add(posBfr.To_str());
  78. rv.Add(rawBfr.To_str());
  79. rv.Add(symBfr.To_str());
  80. if (symList.Count() > 0)
  81. rv.Add("");
  82. for (int i = 0; i < symList.Count(); i++)
  83. rv.Add((String)symList.Get_at(i));
  84. return rv.To_str_ary();
  85. }
  86. List_adp marks = List_adp_.New();
  87. public static GfmlStringHighlighter new_() {
  88. GfmlStringHighlighter rv = new GfmlStringHighlighter();
  89. return rv;
  90. } GfmlStringHighlighter() {}
  91. }
  92. class GfmlStringHighlighterMarker {
  93. public int Pos() {return pos;} public GfmlStringHighlighterMarker Pos_(int v) {pos = v; return this;} int pos;
  94. public char Sym() {return sym;} public GfmlStringHighlighterMarker Sym_(char v) {sym = v; return this;} char sym;
  95. public String Msg() {return msg;} public GfmlStringHighlighterMarker Msg_(String v) {msg = v; return this;} private String msg;
  96. public static final GfmlStringHighlighterMarker Null = new GfmlStringHighlighterMarker().Pos_(-1);
  97. }