From 005136ac54a777639f13bebc9d04ef994fe812a8 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 20 Feb 2017 19:51:02 +0000 Subject: [PATCH] add support for displaying spans --- sourcing/static/css/style.css | 4 ++++ sourcing/templates/head.html | 1 + sourcing/templates/view.html | 41 +++++++++++++++++++++++++---------- sourcing/text.py | 20 +++++++++++++++++ sourcing/view.py | 21 +++++++++++++----- 5 files changed, 70 insertions(+), 17 deletions(-) create mode 100644 sourcing/static/css/style.css diff --git a/sourcing/static/css/style.css b/sourcing/static/css/style.css new file mode 100644 index 0000000..2baf1d1 --- /dev/null +++ b/sourcing/static/css/style.css @@ -0,0 +1,4 @@ +.highlight { + padding: 2px; + background: yellow; +} diff --git a/sourcing/templates/head.html b/sourcing/templates/head.html index 1024ef2..726071d 100644 --- a/sourcing/templates/head.html +++ b/sourcing/templates/head.html @@ -7,6 +7,7 @@ {{ title | default("Xanadu") }} + {{ style | safe }} diff --git a/sourcing/templates/view.html b/sourcing/templates/view.html index 5f38166..e96fc2b 100644 --- a/sourcing/templates/view.html +++ b/sourcing/templates/view.html @@ -9,29 +9,46 @@ div#text { font-family: Courier; }

{{ title }} {% if doc.user == current_user %} - edit + edit {% endif %} {% if doc.type == 'xanadoc' %} - fulfil + fulfil {% endif %}

back to index

+
- {%- for start, line in iter_lines(doc.text) if line -%} -

{{ line }}

- {%- endfor -%} + {% if span_start and span_length %} + {%- for start, line in add_highlight(doc.text, span_start, span_length) if line -%} +

+ {% for i in line %} + {%- if i.highlight -%} + {{- i.highlight -}} + {%- else -%} + {{- i.text -}} + {%- endif -%} + {% endfor %} +

+ {%- endfor -%} + {% else %} + {%- for start, line in iter_lines(doc.text) if line -%} +

{{ line }}

+ {%- endfor -%} + {% endif %}
- {% if doc.type == 'sourcedoc' %} + + {% if doc.type == 'sourcedoc' and not (span_start and span_length) %} show span selector - {% set scripts %} - - - {% endset %} {% endif %}
+{% set scripts %} + + +{% endset %} + {% include "foot.html" %} diff --git a/sourcing/text.py b/sourcing/text.py index cbfea91..b8cb383 100644 --- a/sourcing/text.py +++ b/sourcing/text.py @@ -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 diff --git a/sourcing/view.py b/sourcing/view.py index 3cd01d2..d6dd6d2 100644 --- a/sourcing/view.py +++ b/sourcing/view.py @@ -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('///raw') +def view_item_raw(username, hashid): + return view_item(username, hashid, raw=True) + @bp.route('//') -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) - return Response(item.text[start:length + start], mimetype='text/plain') - - 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('///edit', methods=['GET', 'POST'])