add support for displaying spans

This commit is contained in:
Edward Betts 2017-02-20 19:51:02 +00:00
parent 9cfe7cd06b
commit 005136ac54
5 changed files with 70 additions and 17 deletions

View file

@ -0,0 +1,4 @@
.highlight {
padding: 2px;
background: yellow;
}

View file

@ -7,6 +7,7 @@
<title>{{ title | default("Xanadu") }}</title> <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='bootstrap/css/bootstrap.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
{{ style | safe }} {{ style | safe }}
</head> </head>

View file

@ -9,29 +9,46 @@ div#text { font-family: Courier; }
<div class="col-xs-12"> <div class="col-xs-12">
<h1>{{ title }} <h1>{{ title }}
{% if doc.user == current_user %} {% if doc.user == current_user %}
<a href="{{ doc.edit_url }}" class="btn btn-default">edit</a> <a href="{{ doc.edit_url }}" class="btn btn-default">edit</a>
{% endif %} {% endif %}
{% if doc.type == 'xanadoc' %} {% if doc.type == 'xanadoc' %}
<a href="{{ doc.url }}" class="btn btn-default">fulfil</a> <a href="{{ doc.url }}" class="btn btn-default">fulfil</a>
{% endif %} {% endif %}
</h1> </h1>
<p><a href="{{ url_for('.home') }}">back to index</a></p> <p><a href="{{ url_for('.home') }}">back to index</a></p>
<div class="well" id="text"> <div class="well" id="text">
{%- for start, line in iter_lines(doc.text) if line -%} {% if span_start and span_length %}
<p data-start="{{ start }}">{{ line }}</p> {%- for start, line in add_highlight(doc.text, span_start, span_length) if line -%}
{%- endfor -%} <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> </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> <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> <p id="span-selector" class="hidden">span: <span id="span"></span></p>
{% set scripts %}
<script>
var doc_url = '{{ doc.external_url }}';
</script>
<script src="{{ url_for('static', filename='js/sourcedoc.js') }}"></script>
{% endset %}
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% set scripts %}
<script>
var doc_url = '{{ doc.external_url }}';
</script>
<script src="{{ url_for('static', filename='js/sourcedoc.js') }}"></script>
{% endset %}
{% include "foot.html" %} {% include "foot.html" %}

View file

@ -19,3 +19,23 @@ def censor_text(text):
def random_chr(): def random_chr():
return chr(random.randint(9728, 9983)) return chr(random.randint(9728, 9983))
return ''.join(random_chr() if c.isalnum() else c for c in text) 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

View file

@ -8,7 +8,7 @@ from .model import User, SourceDoc, Item, XanaDoc, XanaLink
from .url import get_url from .url import get_url
from .edl import fulfil_edl_with_sources from .edl import fulfil_edl_with_sources
from .database import session 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 werkzeug.debug.tbtools import get_current_traceback
from jinja2 import evalcontextfilter, Markup from jinja2 import evalcontextfilter, Markup
from functools import wraps from functools import wraps
@ -137,20 +137,31 @@ def view_edl(username, hashid):
return render_template('view.html', doc=item, iter_lines=iter_lines) 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>') @bp.route('/<username>/<hashid>')
def view_item(username, hashid): def view_item(username, hashid, raw=False):
if ',' in hashid: if ',' in hashid:
m = re_spanpointer.match(hashid) m = re_spanpointer.match(hashid)
hashid, start, length = m.group(1), int(m.group(2)), int(m.group(3)) hashid, start, length = m.group(1), int(m.group(2)), int(m.group(3))
item = get_item(username, hashid) item = get_item(username, hashid)
return Response(item.text[start:length + start], mimetype='text/plain') if raw:
return Response(item.text[start:length + start], mimetype='text/plain')
item = get_item(username, hashid) else:
start, length = None, None
item = get_item(username, hashid)
if raw:
return Response(item.text, mimetype='text/plain')
if item.type == 'xanadoc': if item.type == 'xanadoc':
return view_xanadoc(item) return view_xanadoc(item)
return render_template('view.html', return render_template('view.html',
doc=item, doc=item,
span_start=start,
span_length=length,
add_highlight=add_highlight,
iter_lines=iter_lines) iter_lines=iter_lines)
@bp.route('/<username>/<hashid>/edit', methods=['GET', 'POST']) @bp.route('/<username>/<hashid>/edit', methods=['GET', 'POST'])