Skip to content

Commit 3228047

Browse files
committed
I works, still some furtive bugs
1 parent d5731e7 commit 3228047

6 files changed

Lines changed: 371 additions & 387 deletions

File tree

common/src/java/com/github/oeuvres/alix/lucene/spans/DocSnipHiliter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ private static void writeTag(final StringBuilder sb, final int kind, final int s
319319
sb.append("<mark class=\"term match\">");
320320
break;
321321
case TERM_OPEN:
322-
sb.append("<mark class=\"term\">");
322+
sb.append("<mark class=\"term orphan\">");
323323
break;
324324
case TERM_CLOSE:
325325
sb.append("</mark>");

common/src/java/com/github/oeuvres/alix/lucene/spans/HtmlSnippets.java

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ public class HtmlSnippets implements SnippetsConsumer
6868
private final Writer writer;
6969

7070
private int ctx = 10;
71-
private String docCss = "hit";
7271
private String doclineFieldName = "docline";
7372
private String hrefBase = "";
7473
private String hrefExt = "";
7574
private String hrefSearch = "";
76-
private String snipCss = "hit snippet";
7775

7876
private String content;
7977
private Document doc;
@@ -154,28 +152,6 @@ public void docClose(final int docId) throws IOException
154152
writer.flush();
155153
}
156154

157-
/**
158-
* Returns the CSS class applied to the {@code <article>} element.
159-
*
160-
* @return CSS class string
161-
*/
162-
public String docCss()
163-
{
164-
return docCss;
165-
}
166-
167-
/**
168-
* Sets the CSS class applied to the {@code <article>} element.
169-
*
170-
* @param docCss CSS class string
171-
* @return this instance
172-
*/
173-
public HtmlSnippets docCss(final String docCss)
174-
{
175-
this.docCss = docCss;
176-
return this;
177-
}
178-
179155
/**
180156
* Returns the name of the stored field used as the document heading.
181157
*
@@ -211,15 +187,25 @@ public void docOpen(final int docId) throws IOException
211187
{
212188
doc = storedFields.document(docId);
213189
id = doc.get(ALIX_ID);
214-
writer.append("<article id=\"").append(id)
215-
.append("\" data-docid=\"").append(String.valueOf(docId))
216-
.append("\" class=\"").append(docCss).append("\">\n");
190+
writer.append("<article")
191+
.append(" id=\"").append(id).append("\"")
192+
.append(" data-docid=\"").append(String.valueOf(docId)).append("\"")
193+
.append(" class=\"result\"")
194+
.append(">\n");
195+
196+
String url = hrefBase + id + hrefExt + hrefSearch;
217197
if (doclineFieldName != null) {
218198
final String docline = doc.get(doclineFieldName);
219199
if (docline != null) {
220-
writer.append("<h2><a href=\"").append(hrefBase).append(id)
221-
.append(hrefExt).append(hrefSearch).append("\">")
222-
.append(docline).append("</a></h2>\n");
200+
writer.append("<h4")
201+
.append(" data-href=\"").append(url).append("\"")
202+
.append(">\n")
203+
.append("<span>").append(docline).append("</span>\n")
204+
.append("<a")
205+
.append(" href=\"").append(url).append("\"")
206+
.append(" class=\"result-open\"")
207+
.append(">→</a>\n")
208+
.append("</h4>\n");
223209
}
224210
}
225211
}
@@ -243,17 +229,19 @@ public void docSnippets(final int docId, final Snippets snippets) throws IOExcep
243229
{
244230
docOpen(docId);
245231
content = doc.get(contentFieldName);
246-
if (snipLimit > 0 && content != null) {
232+
final int snipCount = snippets.snips4doc();
233+
if (snipLimit > 0 && content != null && snipCount > 0) {
247234
topSnips.clear();
248-
final int snipCount = snippets.snips4doc();
249235
for (int snipOrd = 0; snipOrd < snipCount; snipOrd++) {
250236
final int startPos = Math.max(0, snippets.snipStartPosition(snipOrd) - ctx);
251237
final int endPos = snippets.snipEndPosition(snipOrd) + ctx;
252238
topSnips.push(snipOrd, scoreSnippet(docId, startPos, endPos));
253239
}
240+
writer.append("<ol class=\"snippets\">\n");
254241
for (final TopArray.IdScore pair : topSnips) {
255242
print(snippets, pair.id());
256243
}
244+
writer.append("</ol>\n");
257245
}
258246
docClose(docId);
259247
}
@@ -324,30 +312,6 @@ public HtmlSnippets hrefSearch(final String hrefSearch)
324312
return this;
325313
}
326314

327-
/**
328-
* Returns the CSS class applied to {@code <li>} elements emitted for each
329-
* snippet line.
330-
*
331-
* @return CSS class string
332-
*/
333-
public String snipCss()
334-
{
335-
return snipCss;
336-
}
337-
338-
/**
339-
* Sets the CSS class applied to {@code <li>} elements emitted for each
340-
* snippet line.
341-
*
342-
* @param snipCss CSS class string
343-
* @return this instance
344-
*/
345-
public HtmlSnippets snipCss(final String snipCss)
346-
{
347-
this.snipCss = snipCss;
348-
return this;
349-
}
350-
351315
/**
352316
* Returns the maximum number of snippets rendered per document.
353317
*
@@ -381,9 +345,14 @@ private void print(final Snippets snippets, final int snipOrd) throws IOExceptio
381345
final int leftMatchStartOffset = snippets.matchStartOffset(leftMatchOrd);
382346
final int rightMatchEndOffset = snippets.matchEndOffset(rightMatchOrd);
383347

384-
writer.append("<li class=\"").append(snipCss).append("\"><a href=\"")
385-
.append(hrefBase).append(id).append(hrefExt).append(hrefSearch)
386-
.append("#snippet").append(String.valueOf(snipOrd)).append("\">");
348+
final String url = hrefBase + id + hrefExt + hrefSearch + "#snippet" + snipOrd;
349+
writer
350+
.append("<li")
351+
.append(" class=\"snippet\"")
352+
.append(" data-href=\"").append(url).append("\"")
353+
.append(">")
354+
.append("<p>");
355+
387356

388357
final int leftOffset = Markup.leftBoundary(content, leftMatchStartOffset, ctx, -1);
389358
detagger.detag(writer, content, leftOffset, leftMatchStartOffset);
@@ -406,7 +375,13 @@ private void print(final Snippets snippets, final int snipOrd) throws IOExceptio
406375

407376
final int rightOffset = Markup.rightBoundary(content, rightMatchEndOffset, ctx, -1);
408377
detagger.detag(writer, content, rightMatchEndOffset, rightOffset);
409-
writer.append("</a></li>\n");
378+
// gutter snippet link
379+
writer.append("</p>")
380+
.append("\n<a")
381+
.append(" class=\"snippet-open\"")
382+
.append(" href=\"").append(url).append("\"")
383+
.append("\">→</a>");
384+
writer.append("</li>\n");
410385
}
411386

412387
/**

web/src/main/java/com/github/oeuvres/alix/web/OpDoc.java

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,6 @@ public boolean offer(
5151
return true;
5252
}
5353

54-
@Override
55-
protected void page(LuceneIndex index, HttpServletRequest req, HttpServletResponse resp)
56-
throws IOException
57-
{
58-
59-
resp.setContentType("text/html; charset=UTF-8");
60-
Writer writer = resp.getWriter();
61-
writer.write("""
62-
<!DOCTYPE html>
63-
<html>
64-
<head>
65-
<title>Alix, document</title>
66-
<link rel="stylesheet" type="text/css" href="https://oeuvres.github.io/teinte_theme/teinte.css"/>
67-
</head>
68-
<style>
69-
::highlight(span-hl) { background-color: #ffe08280; }
70-
mark.hit.pivot { font-weight: bold; }
71-
mark.hit.term { background: #c8e6c9; }
72-
</style>
73-
<body>
74-
""");
75-
html(index, req, resp);
76-
writer.write("""
77-
<script>
78-
const spanHl = new Highlight();
79-
document.querySelectorAll('wbr.hl-start').forEach(start => {
80-
const end = document.querySelector(`wbr.hl-end[data-hl="${start.dataset.hl}"]`);
81-
if (!end) return;
82-
const range = new Range();
83-
range.setStartAfter(start);
84-
range.setEndBefore(end);
85-
spanHl.add(range);
86-
});
87-
CSS.highlights.set('span-hl', spanHl);
88-
</script>
89-
</body>
90-
</html>
91-
""");
92-
93-
}
94-
9554
@Override
9655
protected void html(LuceneIndex index, HttpServletRequest request, HttpServletResponse response)
9756
throws IOException
@@ -133,4 +92,45 @@ protected void html(LuceneIndex index, HttpServletRequest request, HttpServletRe
13392
}
13493
writer.write(content);
13594
}
95+
96+
@Override
97+
protected void page(LuceneIndex index, HttpServletRequest req, HttpServletResponse resp)
98+
throws IOException
99+
{
100+
101+
resp.setContentType("text/html; charset=UTF-8");
102+
Writer writer = resp.getWriter();
103+
writer.write("""
104+
<!DOCTYPE html>
105+
<html>
106+
<head>
107+
<title>Alix, document</title>
108+
<link rel="stylesheet" type="text/css" href="https://oeuvres.github.io/teinte_theme/teinte.css"/>
109+
</head>
110+
<style>
111+
::highlight(span-hl) { background-color: #ffe08280; }
112+
mark.hit.pivot { font-weight: bold; }
113+
mark.hit.term { background: #c8e6c9; }
114+
</style>
115+
<body>
116+
""");
117+
html(index, req, resp);
118+
writer.write("""
119+
<script>
120+
const spanHl = new Highlight();
121+
document.querySelectorAll('wbr.hl-start').forEach(start => {
122+
const end = document.querySelector(`wbr.hl-end[data-hl="${start.dataset.hl}"]`);
123+
if (!end) return;
124+
const range = new Range();
125+
range.setStartAfter(start);
126+
range.setEndBefore(end);
127+
spanHl.add(range);
128+
});
129+
CSS.highlights.set('span-hl', spanHl);
130+
</script>
131+
</body>
132+
</html>
133+
""");
134+
135+
}
136136
}

0 commit comments

Comments
 (0)