/usr/share/doc/python-django-haystack-doc/html/highlighting.html is in python-django-haystack-doc 2.8.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Highlighting — Haystack 2.5.0 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '2.5.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Faceting" href="faceting.html" />
<link rel="prev" title="Best Practices" href="best_practices.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="highlighting">
<span id="ref-highlighting"></span><h1>Highlighting<a class="headerlink" href="#highlighting" title="Permalink to this headline">¶</a></h1>
<p>Haystack supports two different methods of highlighting. You can either use
<code class="docutils literal"><span class="pre">SearchQuerySet.highlight</span></code> or the built-in <code class="docutils literal"><span class="pre">{%</span> <span class="pre">highlight</span> <span class="pre">%}</span></code> template tag,
which uses the <code class="docutils literal"><span class="pre">Highlighter</span></code> class. Each approach has advantages and
disadvantages you need to weigh when deciding which to use.</p>
<p>If you want portable, flexible, decently fast code, the
<code class="docutils literal"><span class="pre">{%</span> <span class="pre">highlight</span> <span class="pre">%}</span></code> template tag (or manually using the underlying
<code class="docutils literal"><span class="pre">Highlighter</span></code> class) is the way to go. On the other hand, if you care more
about speed and will only ever be using one backend,
<code class="docutils literal"><span class="pre">SearchQuerySet.highlight</span></code> may suit your needs better.</p>
<p>Use of <code class="docutils literal"><span class="pre">SearchQuerySet.highlight</span></code> is documented in the
<a class="reference internal" href="searchqueryset_api.html"><span class="doc">SearchQuerySet API</span></a> documentation and the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">highlight</span> <span class="pre">%}</span></code> tag is
covered in the <a class="reference internal" href="templatetags.html"><span class="doc">Template Tags</span></a> documentation, so the rest of this material
will cover the <code class="docutils literal"><span class="pre">Highlighter</span></code> implementation.</p>
<div class="section" id="highlighter">
<h2><code class="docutils literal"><span class="pre">Highlighter</span></code><a class="headerlink" href="#highlighter" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">Highlighter</span></code> class is a pure-Python implementation included with Haystack
that’s designed for flexibility. If you use the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">highlight</span> <span class="pre">%}</span></code> template
tag, you’ll be automatically using this class. You can also use it manually in
your code. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">haystack.utils.highlighting</span> <span class="k">import</span> <span class="n">Highlighter</span>
<span class="gp">>>> </span><span class="n">my_text</span> <span class="o">=</span> <span class="s1">'This is a sample block that would be more meaningful in real life.'</span>
<span class="gp">>>> </span><span class="n">my_query</span> <span class="o">=</span> <span class="s1">'block meaningful'</span>
<span class="gp">>>> </span><span class="n">highlight</span> <span class="o">=</span> <span class="n">Highlighter</span><span class="p">(</span><span class="n">my_query</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">highlight</span><span class="o">.</span><span class="n">highlight</span><span class="p">(</span><span class="n">my_text</span><span class="p">)</span>
<span class="go">u'...<span class="highlighted">block</span> that would be more <span class="highlighted">meaningful</span> in real life.'</span>
</pre></div>
</div>
<p>The default implementation takes three optional kwargs: <code class="docutils literal"><span class="pre">html_tag</span></code>,
<code class="docutils literal"><span class="pre">css_class</span></code> and <code class="docutils literal"><span class="pre">max_length</span></code>. These allow for basic customizations to the
output, like so:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">haystack.utils.highlighting</span> <span class="k">import</span> <span class="n">Highlighter</span>
<span class="gp">>>> </span><span class="n">my_text</span> <span class="o">=</span> <span class="s1">'This is a sample block that would be more meaningful in real life.'</span>
<span class="gp">>>> </span><span class="n">my_query</span> <span class="o">=</span> <span class="s1">'block meaningful'</span>
<span class="gp">>>> </span><span class="n">highlight</span> <span class="o">=</span> <span class="n">Highlighter</span><span class="p">(</span><span class="n">my_query</span><span class="p">,</span> <span class="n">html_tag</span><span class="o">=</span><span class="s1">'div'</span><span class="p">,</span> <span class="n">css_class</span><span class="o">=</span><span class="s1">'found'</span><span class="p">,</span> <span class="n">max_length</span><span class="o">=</span><span class="mi">35</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">highlight</span><span class="o">.</span><span class="n">highlight</span><span class="p">(</span><span class="n">my_text</span><span class="p">)</span>
<span class="go">u'...<div class="found">block</div> that would be more <div class="found">meaningful</div>...'</span>
</pre></div>
</div>
<p>Further, if this implementation doesn’t suit your needs, you can define your own
custom highlighter class. As long as it implements the API you’ve just seen, it
can highlight however you choose. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># In ``myapp/utils.py``...</span>
<span class="kn">from</span> <span class="nn">haystack.utils.highlighting</span> <span class="k">import</span> <span class="n">Highlighter</span>
<span class="k">class</span> <span class="nc">BorkHighlighter</span><span class="p">(</span><span class="n">Highlighter</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">render_html</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">highlight_locations</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">start_offset</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">end_offset</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="n">highlighted_chunk</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">text_block</span><span class="p">[</span><span class="n">start_offset</span><span class="p">:</span><span class="n">end_offset</span><span class="p">]</span>
<span class="k">for</span> <span class="n">word</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">query_words</span><span class="p">:</span>
<span class="n">highlighted_chunk</span> <span class="o">=</span> <span class="n">highlighted_chunk</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">word</span><span class="p">,</span> <span class="s1">'Bork!'</span><span class="p">)</span>
<span class="k">return</span> <span class="n">highlighted_chunk</span>
</pre></div>
</div>
<p>Then set the <code class="docutils literal"><span class="pre">HAYSTACK_CUSTOM_HIGHLIGHTER</span></code> setting to
<code class="docutils literal"><span class="pre">myapp.utils.BorkHighlighter</span></code>. Usage would then look like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">highlight</span> <span class="o">=</span> <span class="n">BorkHighlighter</span><span class="p">(</span><span class="n">my_query</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">highlight</span><span class="o">.</span><span class="n">highlight</span><span class="p">(</span><span class="n">my_text</span><span class="p">)</span>
<span class="go">u'Bork! that would be more Bork! in real life.'</span>
</pre></div>
</div>
<p>Now the <code class="docutils literal"><span class="pre">{%</span> <span class="pre">highlight</span> <span class="pre">%}</span></code> template tag will also use this highlighter.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="toc.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Highlighting</a><ul>
<li><a class="reference internal" href="#highlighter"><code class="docutils literal"><span class="pre">Highlighter</span></code></a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="toc.html">Documentation overview</a><ul>
<li><a href="index.html">Welcome to Haystack!</a><ul>
<li>Previous: <a href="best_practices.html" title="previous chapter">Best Practices</a></li>
<li>Next: <a href="faceting.html" title="next chapter">Faceting</a></li>
</ul></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/highlighting.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2009-2018, Daniel Lindsley.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.7</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
|
<a href="_sources/highlighting.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>
|