KeywordInContextIndexBuilder.st
changeset 4126 4d3ec803fddf
parent 4125 d597206782cc
child 4127 0f3c785bb689
--- a/KeywordInContextIndexBuilder.st	Thu Oct 13 12:44:38 2016 +0200
+++ b/KeywordInContextIndexBuilder.st	Thu Oct 13 12:57:35 2016 +0200
@@ -38,19 +38,18 @@
 
 documentation
 "
-    A support class for building a KWIC (Keyword in Context) index.
-    (for example, to build a KWIC index on html pages or class documentation).
+    A support class for building KWIC (Keyword in Context) or KWOC (Keyword out of Context) indexes.
+    (for example, to build such indexes on html pages or class documentation).
     
     To generate a kwic, add each line together with a reference (or page number, or whatever),
     using addLine:reference:.
-    Then, when finished, enumerate the kwic.
+    Then, when finished, enumerate the kwic and print as kwic or kwoc.
 
     [author:]
         Claus Gittinger (cg@alan)
 
-    [instance variables:]
-
-    [class variables:]
+    [examples:]
+        see examples method
 
     [see also:]
         https://en.wikipedia.org/wiki/Key_Word_in_Context (english)
@@ -61,6 +60,7 @@
 
 examples
 "
+    building a kwic; print as kwic and kwoc
                                                                 [exBegin]
     |kwic|
 
@@ -68,12 +68,13 @@
     kwic excluded:#('the' 'and' 'a' 'an').
 
     kwic addLine:'bla bla bla' reference:1.
-    kwic addLine:'foo, bar. baz' reference:1.
-    kwic addLine:'one two three' reference:2.
-    kwic addLine:'a cat and a dog' reference:3.
-    kwic addLine:'the man in the middle' reference:4.
-    kwic addLine:'the man with the dog' reference:5.
+    kwic addLine:'foo, bar. baz' reference:2.
+    kwic addLine:'one two three' reference:3.
+    kwic addLine:'a cat and a dog' reference:4.
+    kwic addLine:'the man in the middle' reference:5.
+    kwic addLine:'the man with the dog' reference:6.
 
+    Transcript showCR:'Printed as KWIC:'.
     kwic 
         entriesDo:[:word :left :right :ref |
             Transcript 
@@ -86,6 +87,19 @@
                 show:'['; show:ref; show:']';
                 cr    
         ].
+
+    Transcript cr.
+    Transcript showCR:'Printed as KWOC:'.
+    kwic 
+        entriesDo:[:word :left :right :ref :line |
+            Transcript 
+                show:((word contractTo:10) paddedTo:10) allBold;
+                space;
+                show:((line contractTo:60) paddedTo:60);
+                space;
+                show:'['; show:ref; show:']';
+                cr    
+        ].
                                                                 [exEnd]
 
 
@@ -300,9 +314,23 @@
 
 !KeywordInContextIndexBuilder methodsFor:'enumerating'!
 
-entriesDo:aFourArgBlock
-    "evaluate the argument, aFourArgBlock for each triple of kwic-word, left-text, right text and reference"
+entriesDo:aFourOrFiveArgBlock
+    "evaluate the argument, for each entry.
+     If it is a 4-arg block, it is called with:
+        kwic-word, 
+        left-text, 
+        right text 
+        and reference
+     If it is a 5-arg block, the original text is passed as additional argument.
+     (stupid, but done for backward compatibility)"
 
+    |fourArgBlock fiveArgBlock|
+
+    aFourOrFiveArgBlock numArgs == 5 ifTrue:[
+        fiveArgBlock := aFourOrFiveArgBlock 
+    ] ifFalse:[
+        fourArgBlock := aFourOrFiveArgBlock 
+    ].    
     keywordToLinesMapping keys asSortedCollection do:[:eachKey |
         |setOfMatches lcKey|
 
@@ -330,7 +358,11 @@
                 left := (context copyTo:lIdx - 1) withoutSeparators.
                 right := (context copyFrom:lIdx + lcKey size) withoutSeparators.
                 word := (context copyFrom:lIdx to:lIdx + lcKey size - 1) withoutSeparators.
-                aFourArgBlock value:word value:left value:right value:ref.
+                fourArgBlock notNil ifTrue:[
+                    fourArgBlock value:word value:left value:right value:ref.
+                ] ifFalse:[
+                    fiveArgBlock value:word value:left value:right value:ref value:text.
+                ].    
             ].
         ]
     ]