File: outhtml_keyword_matcher.lua

package info (click to toggle)
highlight 4.10-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,556 kB
  • sloc: cpp: 27,579; makefile: 411; sh: 341; ansic: 264; php: 236; python: 217; ruby: 132; perl: 61; tcl: 1
file content (81 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81


Description="Shows matching keywords in HTML output. Define the keyword group as plug-in parameter."

Categories = {"format", "html", "usability" }

-- optional parameter: syntax description
function syntaxUpdate(desc)

  if (HL_OUTPUT == HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then

    -- can use 1 to 4, depending on the syntax definition
    keywordGroup=1
    if (#HL_PLUGIN_PARAM>0) then keywordGroup = tonumber(HL_PLUGIN_PARAM) end

    kwID = { }
    kwIDCnt=0

    HeaderInjection=[[
      <script type="text/javascript">
      function showSameKW(sender){
        var myRegexp = /(kw_\d+)/;
        var kwID = myRegexp.exec(sender.id)[1];
        var elements=document.getElementsByTagName('span');
        for (var i = 0; i < elements.length; i++) {
          if (elements[i].id.indexOf( kwID)==0 && myRegexp.exec(elements[i].id)[1] ==  kwID){
            elements[i].style.background= (elements[i].style.background=='') ? 'yellow': '';
          }
        }
      }
      </script>
]]
  end


  if OnStateChange ~= nil then
      OrigOnStateChange = OnStateChange;
  end

  --may be triggered twice per keyword
  function OnStateChange(oldState, newState, token, kwgroup, lineno, column)
    if newState==HL_KEYWORD  and kwgroup==keywordGroup then

      if kwID[token] == nil then
        kwIDCnt=kwIDCnt+1
        kwID[token] = { }
        kwID[token][0] = kwIDCnt
        kwID[token][1] = 1
      else
        kwID[token][1] = kwID[token][1] + 1
      end

    end
    if OrigOnStateChange then
        return OrigOnStateChange(oldState, newState, token, kwgroup, lineno, column)
    end
    return newState
  end

  function Decorate(token, state)
    if (state ~= HL_KEYWORD or kwID[token]==nil or HL_OUTPUT ~= HL_FORMAT_HTML) then
      return
    end
    return '<span class="hl box" id="kw_'..kwID[token][0]..'_'..kwID[token][1]..'" onclick="showSameKW(this);">'..token..'</span>'
  end

end


function themeUpdate(desc)
  if (HL_OUTPUT == HL_FORMAT_HTML or HL_OUTPUT == HL_FORMAT_XHTML) then
    Injections[#Injections+1]=".hl.box { border-width:1px;border-style:dotted;border-color:gray; cursor: pointer;}"
  end
end

Plugins={

  { Type="lang", Chunk=syntaxUpdate },
  { Type="theme", Chunk=themeUpdate },

}