Skip to content

Commit

Permalink
Support config.templates.linenums
Browse files Browse the repository at this point in the history
This option allows to show line numbers on source code.
The option is the same as used in https://github.com/terryweiss/docstrap
  • Loading branch information
vmeurisse authored and twidi committed Nov 24, 2014
1 parent ea2c790 commit 47f3ec1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ You can set options for customizing your documentations.
"title": "",
"description": "",
"keyword": ""
}
},
"linenums": true
}
```

Expand Down
5 changes: 3 additions & 2 deletions conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
"title": "",
"description": "",
"keyword": ""
}
},
"linenums": false
},
"markdown": {
"parser": "gfm",
"hardwrap": true,
"tags": ["examples"]
}
}
}
26 changes: 19 additions & 7 deletions static/scripts/linenumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@
var source = document.getElementsByClassName('prettyprint source');

if (source && source[0]) {
source = source[0].getElementsByTagName('code')[0];
var linenums = config.linenums;

numbered = source.innerHTML.split('\n');
numbered = numbered.map(function(item) {
counter++;
return '<span id="line' + counter + '"></span>' + item;
});
if (linenums) {
source = source[0].getElementsByTagName('ol')[0];

source.innerHTML = numbered.join('\n');
numbered = Array.prototype.slice.apply(source.children);
numbered = numbered.map(function(item) {
counter++;
item.id = 'line' + counter;
});
} else {
source = source[0].getElementsByTagName('code')[0];

numbered = source.innerHTML.split('\n');
numbered = numbered.map(function(item) {
counter++;
return '<span id="line' + counter + '"></span>' + item;
});

source.innerHTML = numbered.join('\n');
}
}
})();
4 changes: 2 additions & 2 deletions tmpl/source.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
?>
<section>
<article>
<pre class="prettyprint source"><code><?js= data.code ?></code></pre>
<pre class="prettyprint source <?js= env.conf.templates.linenums ? 'linenums' : '' ?>"><code><?js= data.code ?></code></pre>
</article>
</section>
</section>

0 comments on commit 47f3ec1

Please sign in to comment.