Skip to content

Commit

Permalink
File Highlight: allow to specify the language (takes precedence over …
Browse files Browse the repository at this point in the history
…extension guess) + Set language on parent even if unknown. Fix #607
  • Loading branch information
Golmote committed Jun 25, 2015
1 parent f1705eb commit 8030db9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
8 changes: 4 additions & 4 deletions components/prism-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ var _ = self.Prism = {
grammar = _.languages[language];
}

if (!grammar) {
return;
}

// Set language on the element, if not present
element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;

Expand All @@ -175,6 +171,10 @@ var _ = self.Prism = {
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
}

if (!grammar) {
return;
}

var code = element.textContent;

if(!code) {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-core.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions plugins/file-highlight/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h1>How to use</h1>

<section>
<h1>Examples</h1>

<p>The plugin’s JS code:</p>
<pre data-src="plugins/file-highlight/prism-file-highlight.js"></pre>

Expand All @@ -53,7 +53,7 @@ <h1>Examples</h1>
<footer data-src="templates/footer.html" data-type="text/html"></footer>

<script src="prism.js"></script>
<script src="plugins/file-highlight/prism-file-highlight.js"></script>
<!-- The File Highlight plugin is already included into Prism.js build -->
<script src="utopia.js"></script>
<script src="components.js"></script>
<script src="code.js"></script>
Expand Down
17 changes: 15 additions & 2 deletions plugins/file-highlight/prism-file-highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@

Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function(pre) {
var src = pre.getAttribute('data-src');
var extension = (src.match(/\.(\w+)$/) || [,''])[1];
var language = Extensions[extension] || extension;

var language, parent = pre;
var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
while (parent && !lang.test(parent.className)) {
parent = parent.parentNode;
}

if (parent) {
language = (pre.className.match(lang) || [,''])[1];
}

if (!language) {
var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
language = Extensions[extension] || extension;
}

var code = document.createElement('code');
code.className = 'language-' + language;
Expand Down
2 changes: 1 addition & 1 deletion plugins/file-highlight/prism-file-highlight.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 19 additions & 6 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,6 @@ var _ = self.Prism = {
grammar = _.languages[language];
}

if (!grammar) {
return;
}

// Set language on the element, if not present
element.className = element.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;

Expand All @@ -180,6 +176,10 @@ var _ = self.Prism = {
parent.className = parent.className.replace(lang, '').replace(/\s+/g, ' ') + ' language-' + language;
}

if (!grammar) {
return;
}

var code = element.textContent;

if(!code) {
Expand Down Expand Up @@ -648,8 +648,21 @@ if (Prism.languages.markup) {

Array.prototype.slice.call(document.querySelectorAll('pre[data-src]')).forEach(function(pre) {
var src = pre.getAttribute('data-src');
var extension = (src.match(/\.(\w+)$/) || [,''])[1];
var language = Extensions[extension] || extension;

var language, parent = pre;
var lang = /\blang(?:uage)?-(?!\*)(\w+)\b/i;
while (parent && !lang.test(parent.className)) {
parent = parent.parentNode;
}

if (parent) {
language = (pre.className.match(lang) || [,''])[1];
}

if (!language) {
var extension = (src.match(/\.(\w+)$/) || [, ''])[1];
language = Extensions[extension] || extension;
}

var code = document.createElement('code');
code.className = 'language-' + language;
Expand Down

0 comments on commit 8030db9

Please sign in to comment.