Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bush: fix links to line numbers and syntax highlighting #2122

Merged
merged 3 commits into from Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 34 additions & 19 deletions lib/html/static/js/search.js
Expand Up @@ -56,7 +56,7 @@ var FileLoader = ( function () {

// asynchronously load file
$.ajax({
mathod: 'POST',
method: 'POST',
url: url,
data: kwargs,
success: function (result) {
Expand All @@ -77,28 +77,36 @@ var FileLoader = ( function () {
};


var selectLine = function (num) {
this.selectLine = function (num) {
/* Highlights and scrolls to the line of code with the provided
* number. */
// insert highlight tags to selected line
var lines = $('#filecode').html().split('\n');
if (num < lines.length) {
lines[num - 1] = mark[0] + lines[num - 1] + mark[1];
num = parseInt(num);

if (previous_search) {
loadFile("", num);
return;
}

// Remove previously highlighted lines.
var highlights = $($('#filecode')[0]).find('span.highlight');
for (high=0; high<highlights.length; high++) {
$(highlights[high]).replaceWith(highlights[high].innerHTML);
}

// Extract text lines from document
var lines = $('#filecode')[0].innerHTML.split('\n');

// Mark highlighted line.
lines[num - 1] = mark[0] + lines[num - 1] + mark[1];

// Set filecode html content.
$('#filecode').html(lines.join('\n'));

// scroll to selected line
$('html, body').animate({scrollTop: $('#' + num).offset().top}, 500);
};


this.gotoLine = function (num) {
/* Reloads the whole file and propts loadFile() to scroll to the
* specified line. */
loadFile("", num);
previous_search = null;
};


this.fileSearch = function () {
/* Called on submit by form:file_search to initiate a search. */
var search_string = document.forms['file-search']['search-string'].value;
Expand Down Expand Up @@ -130,20 +138,20 @@ var FileLoader = ( function () {
/* Attatch handler on .line-number to goto the selected line number
* when clicked. */
$('.line-number').on('click', function () {
FileLoader.gotoLine(parseFloat($(this).attr('id')))
FileLoader.selectLine(parseFloat($(this).attr('id')));
});
}


var self = {
/* Export public functions. */
gotoLine: this.gotoLine,
selectLine: this.selectLine,
fileSearch: this.fileSearch,
init: this.init,
bindLineNumbers: this.bindLineNumbers
};


return self;

})();
Expand All @@ -152,7 +160,6 @@ var FileLoader = ( function () {
$(function () {
// load file once all resources (incl FileLoader) have been obtained
FileLoader.init();
//FileLoader.fileSearch();
FileLoader.bindLineNumbers();
$('form[name="file-search"]').on('submit', function () {
return FileLoader.fileSearch();
Expand All @@ -163,6 +170,14 @@ $(function () {
$('form[name=file-search]').find('button span.toggle-target')
.html(ele.html());
$(document.forms['file-search']['search-mode']).val(ele.data('value'));

// scroll to line if specified in URL
$(document).ready(function (){
var hash = window.location.hash;
if (hash) {
FileLoader.selectLine(hash.split('#')[1]);
}
});
});


Expand Down
24 changes: 11 additions & 13 deletions lib/html/template/rose-bush/view-search.html
Expand Up @@ -6,7 +6,7 @@
<div class="col-md-1 text-right">
<pre id="filelinenumbers" class="prettyprint">
{% for num in line_numbers -%}
<span><a id="{{num}}" class="line-number">{{num}}</a></span>
<span><a id="{{num}}" href="#{{num}}" class="line-number">{{num}}</a></span>
{% endfor -%}
</pre>
</div>
Expand All @@ -19,21 +19,19 @@
<div class="col-md-11">
{% endif -%}
<pre id="filecode" {% if file_content %}class="prettyprint lang-{{file_content}}"{% endif -%}>
{% set PREFIXES = {
"[DEBUG]": "muted",
"[FAIL] ": "text-danger",
"[INFO] ": "text-info",
"[ OK ] ": "text-success",
"[WARN] ": "text-warning",
} -%}
{% for line in lines -%}
{% for part in line -%}
{% if loop.index == 1 and part|length > 7 and part[0:7] in PREFIXES
-%}<span class="{{PREFIXES[part[0:7]]}}">{{part[0:7]}}</span>{{part[7:]}}{%
else -%}
{% if loop.index % 2 == 1 -%}{{part}}{% else
-%}<span class="highlight">{{part}}</span>{% endif -%}
{% if not file_content -%}
{% set part = part | replace(
'DEBUG', '<span class="nocode text-muted">DEBUG</span>') | replace(
'INFO', '<span class="nocode text-info">INFO</span>') | replace(
'ERROR', '<span class="nocode text-dander">ERROR</span>') | replace(
'CRITICAL', '<span class="nocode text-dander">CRITICAL</span>') | replace(
'WARNING', '<span class="nocode text-warning">WARNING</span>') -%}
{% endif -%}
{% if loop.index % 2 == 1 -%}{{part}}{% else
-%}<span class="highlight">{{part}}</span>{%
endif -%}
{% endfor -%}
{{ "" }}
{% endfor -%}
Expand Down
21 changes: 9 additions & 12 deletions lib/html/template/rose-bush/view.html
Expand Up @@ -60,24 +60,21 @@ <h1>{{path}}</h1>
</pre>
</div>
<div class="col-md-11">
<pre{% if file_content %} class="prettyprint lang-{{file_content}}"{% endif %}>
{%- set PREFIXES = {
"[DEBUG]": "muted",
"[FAIL] ": "text-danger",
"[INFO] ": "text-info",
"[ OK ] ": "text-success",
"[WARN] ": "text-warning",
} -%}
<pre id="filecode"{% if file_content %} class="prettyprint lang-{{file_content}}"{% endif %}>
{% for line in lines -%}
{% if line|length > 7 and line[0:7] in PREFIXES -%}
<span class="{{PREFIXES[line[0:7]]}}">{{line[0:7]}}</span>{{line[7:]}}
{% else -%}
{% if not file_content -%}
{% set line = line | replace(
'DEBUG', '<span class="nocode text-muted">DEBUG</span>') | replace(
'INFO', '<span class="nocode text-info">INFO</span>') | replace(
'ERROR', '<span class="nocode text-dander">ERROR</span>') | replace(
'CRITICAL', '<span class="nocode text-dander">CRITICAL</span>') | replace(
'WARNING', '<span class="nocode text-warning">WARNING</span>') -%}
{% endif -%}
{% if mode == "tags" -%}
{{line}}
{% else -%}
{{line|urlise}}
{% endif -%}
{% endif -%}
{% endfor -%}
</pre>
</div>
Expand Down