add support for displaying spans
This commit is contained in:
parent
9cfe7cd06b
commit
005136ac54
4
sourcing/static/css/style.css
Normal file
4
sourcing/static/css/style.css
Normal file
|
@ -0,0 +1,4 @@
|
|||
.highlight {
|
||||
padding: 2px;
|
||||
background: yellow;
|
||||
}
|
|
@ -7,6 +7,7 @@
|
|||
<title>{{ title | default("Xanadu") }}</title>
|
||||
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='bootstrap/css/bootstrap.css') }}">
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
|
||||
{{ style | safe }}
|
||||
</head>
|
||||
|
||||
|
|
|
@ -16,22 +16,39 @@ div#text { font-family: Courier; }
|
|||
{% endif %}
|
||||
</h1>
|
||||
<p><a href="{{ url_for('.home') }}">back to index</a></p>
|
||||
|
||||
<div class="well" id="text">
|
||||
{% if span_start and span_length %}
|
||||
{%- for start, line in add_highlight(doc.text, span_start, span_length) if line -%}
|
||||
<p data-start="{{ start }}">
|
||||
{% for i in line %}
|
||||
{%- if i.highlight -%}
|
||||
<span class="highlight">{{- i.highlight -}}</span>
|
||||
{%- else -%}
|
||||
{{- i.text -}}
|
||||
{%- endif -%}
|
||||
{% endfor %}
|
||||
</p>
|
||||
{%- endfor -%}
|
||||
{% else %}
|
||||
{%- for start, line in iter_lines(doc.text) if line -%}
|
||||
<p data-start="{{ start }}">{{ line }}</p>
|
||||
{%- endfor -%}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% if doc.type == 'sourcedoc' %}
|
||||
|
||||
{% if doc.type == 'sourcedoc' and not (span_start and span_length) %}
|
||||
<a href="#" id="show-span-selector" class="btn btn-default">show span selector</a>
|
||||
<p id="span-selector" class="hidden">span: <span id="span"></span></p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% set scripts %}
|
||||
<script>
|
||||
var doc_url = '{{ doc.external_url }}';
|
||||
</script>
|
||||
<script src="{{ url_for('static', filename='js/sourcedoc.js') }}"></script>
|
||||
{% endset %}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% include "foot.html" %}
|
||||
|
|
|
@ -19,3 +19,23 @@ def censor_text(text):
|
|||
def random_chr():
|
||||
return chr(random.randint(9728, 9983))
|
||||
return ''.join(random_chr() if c.isalnum() else c for c in text)
|
||||
|
||||
def add_highlight(text, span_start, span_length):
|
||||
span_end = span_start + span_length
|
||||
for start, line in iter_lines(text):
|
||||
end = start + len(line)
|
||||
if not ((end > span_start) and (start < span_end)):
|
||||
yield start, [{'text': line}]
|
||||
continue
|
||||
|
||||
within_line = span_start - start
|
||||
cur = []
|
||||
if within_line > 0:
|
||||
before = line[:within_line]
|
||||
cur.append({'text': before})
|
||||
cur.append({'highlight': line[max(within_line, 0):within_line + span_length]})
|
||||
after = line[within_line + span_length:]
|
||||
if after:
|
||||
cur.append({'text': after})
|
||||
|
||||
yield start, cur
|
||||
|
|
|
@ -8,7 +8,7 @@ from .model import User, SourceDoc, Item, XanaDoc, XanaLink
|
|||
from .url import get_url
|
||||
from .edl import fulfil_edl_with_sources
|
||||
from .database import session
|
||||
from .text import iter_lines
|
||||
from .text import iter_lines, add_highlight
|
||||
from werkzeug.debug.tbtools import get_current_traceback
|
||||
from jinja2 import evalcontextfilter, Markup
|
||||
from functools import wraps
|
||||
|
@ -137,20 +137,31 @@ def view_edl(username, hashid):
|
|||
|
||||
return render_template('view.html', doc=item, iter_lines=iter_lines)
|
||||
|
||||
@bp.route('/<username>/<hashid>/raw')
|
||||
def view_item_raw(username, hashid):
|
||||
return view_item(username, hashid, raw=True)
|
||||
|
||||
@bp.route('/<username>/<hashid>')
|
||||
def view_item(username, hashid):
|
||||
def view_item(username, hashid, raw=False):
|
||||
if ',' in hashid:
|
||||
m = re_spanpointer.match(hashid)
|
||||
hashid, start, length = m.group(1), int(m.group(2)), int(m.group(3))
|
||||
item = get_item(username, hashid)
|
||||
if raw:
|
||||
return Response(item.text[start:length + start], mimetype='text/plain')
|
||||
|
||||
else:
|
||||
start, length = None, None
|
||||
item = get_item(username, hashid)
|
||||
if raw:
|
||||
return Response(item.text, mimetype='text/plain')
|
||||
if item.type == 'xanadoc':
|
||||
return view_xanadoc(item)
|
||||
|
||||
return render_template('view.html',
|
||||
doc=item,
|
||||
span_start=start,
|
||||
span_length=length,
|
||||
add_highlight=add_highlight,
|
||||
iter_lines=iter_lines)
|
||||
|
||||
@bp.route('/<username>/<hashid>/edit', methods=['GET', 'POST'])
|
||||
|
|
Loading…
Reference in a new issue