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

Auto-detect command lines if 'data-filter' attribute present #1358

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion plugins/command-line/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ <h1>How to use</h1>

<p>Add class <strong>command-line</strong> to your <code class="language-markup">&lt;pre></code>. For a server command line, specify the user and host names using the <code class="language-markup">data-user</code> and <code class="language-markup">data-host</code> attributes. The resulting prompt displays a <strong>#</strong> for the root user and <strong>$</strong> for all other users. For any other command line, such as a Windows prompt, you may specify the entire prompt using the <code class="language-markup">data-prompt</code> attribute.</p>

<p>Optional: You may ask the plugin to scan the content in a <code class="language-markup">&lt;code></code> block to detect and format commands and responses. Add a <code class="language-markup">data-filter</code> attribute on the <code class="language-markup">&lt;pre></code> element with a non-empty value. All lines that start with the value in the <code class="language-markup">data-prompt</code> attribute will be taken as a shell command, and those that do not will be treated as shell output.</p>

<p>Optional: You may specify the lines to be presented as output (no prompt) through the <code class="language-markup">data-output</code> attribute on the <code class="language-markup">&lt;pre></code> element in the following simple format:</p>
<ul>
<li>A single number refers to the line with that number</li>
Expand Down Expand Up @@ -93,6 +95,14 @@ <h2>Windows PowerShell With Output</h2>
d-r-- 10/14/2015 5:06 PM Searches
d-r-- 10/14/2015 5:06 PM Videos</code></pre>

<h2>Automatic Formatting with data-filter Attribute</h2>
<pre class="command-line" data-prompt="user%" data-filter="1"><code class="language-bash">user% ls -l
total 40
-rw-r--r-- 1 howes staff 5466 Mar 15 15:04 index.html
-rw-r--r-- 1 howes staff 724 Mar 15 14:56 prism-command-line.css
-rw-r--r-- 1 howes staff 3312 Mar 15 14:58 prism-command-line.js
-rw-r--r-- 1 howes staff 1243 Mar 15 14:30 prism-command-line.min.js</code></pre>

</section>

<footer data-src="templates/footer.html" data-type="text/html"></footer>
Expand All @@ -106,4 +116,4 @@ <h2>Windows PowerShell With Output</h2>
<script src="components/prism-powershell.js"></script>

</body>
</html>
</html>
4 changes: 4 additions & 0 deletions plugins/command-line/prism-command-line.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@
.command-line-prompt > span[data-prompt]:before {
content: attr(data-prompt);
}

.command-line-command {
color: #3f3;
}
23 changes: 22 additions & 1 deletion plugins/command-line/prism-command-line.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ Prism.hooks.add('complete', function (env) {
};

// Create the "rows" that will become the command-line prompts. -- cwells
var lines = new Array(1 + env.code.split('\n').length);
var content = env.code.split('\n');
var lines = new Array(1 + content.length);
var promptText = getAttribute('data-prompt', '');
if (promptText !== '') {
lines = lines.join('<span data-prompt="' + promptText + '"></span>');
Expand All @@ -54,6 +55,26 @@ Prism.hooks.add('complete', function (env) {
prompt.className = 'command-line-prompt';
prompt.innerHTML = lines;

var filterContent = getAttribute('data-filter', '');
if (filterContent.length > 0) {
for (var i = 0; i < content.length; i++) {
var line = content[i];
if (line.slice(0, promptText.length) == promptText) {
// We have a command -- strip off the prompt from the source text and wrap in <span>
content[i] = '<span class="command-line-command">' + line.slice(promptText.length + 1) +
'</span>';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly, it means users who don't use the data-filter attribute won't benefit from the styled <span>. Am I right? Couldn't this behaviour be added in every cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not want to cause any possibly breaking change, hence the clear separation. I am more than willing to rework it if desired.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be better to have a predictible behaviour when the span is always present, indeed. But yes we need to make sure nothing breaks.

}
else {
// We have output -- strip off the prompt tags for the line
var node = prompt.children[i];
node.removeAttribute('data-user');
node.removeAttribute('data-host');
node.removeAttribute('data-prompt');
}
}
env.element.innerHTML = content.join('\n');
}

// Mark the output lines so they can be styled differently (no prompt). -- cwells
var outputSections = pre.getAttribute('data-output') || '';
outputSections = outputSections.split(',');
Expand Down
2 changes: 1 addition & 1 deletion plugins/command-line/prism-command-line.min.js

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