Command Line: Add support for command continuation prefix (#3344)

This commit is contained in:
at055612 2022-02-23 11:30:35 +00:00 committed by GitHub
parent 4eb928c33e
commit b53832cd1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 22 deletions

View File

@ -25,7 +25,8 @@
<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 specify the lines to be presented as output (no prompt and no highlighting) 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>
<h2>Optional: Command output (positional)</h2>
<p>You may specify the lines to be presented as output (no prompt and no highlighting) 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>
<li>Ranges are denoted by two numbers, separated with a hyphen (-)</li>
@ -48,26 +49,60 @@
<dd>Lines 1 through 2, line 5, lines 9 through 20</dd>
</dl>
<p>Optional: To automatically present some lines as output, you can prefix those lines with any string and specify the prefix using the <code class="language-markup">data-filter-output</code> attribute on the <code class="language-markup">&lt;pre></code> element. For example, <code class="language-markup">data-filter-output="(out)"</code> will treat lines beginning with <code class="language-markup">(out)</code> as output and remove the prefix.</p>
<h2>Optional: Command output (prefix)</h2>
<p>To automatically present some lines as output, you can prefix those lines with any string and specify the prefix using the <code class="language-markup">data-filter-output</code> attribute on the <code class="language-markup">&lt;pre></code> element. For example, <code class="language-markup">data-filter-output="(out)"</code> will treat lines beginning with <code class="language-markup">(out)</code> as output and remove the prefix.</p>
<p>Output lines are user selectable by default, so if you select the whole content of the code block, it will select the shell commands and any output lines. This may not be desireable if you want to copy/paste just the commands and not the output. If you want to make the output not user selectable then add the following to your CSS:</p>
<p>A blank line will render as an empty line with a prompt. If you want an empty line without a prompt then you can use a line containing just the output prefix, e.g. <code class="language-markup">(out)</code>. See the blank lines in the examples below.</p>
<p>Output lines are user selectable by default, so if you select the whole content of the code block, it will select the shell commands and any output lines. This may not be desirable if you want to copy/paste just the commands and not the output. If you want to make the output not user selectable then add the following to your CSS:</p>
<pre><code class="language-css">.command-line span.token.output {
user-select: none;
}</code></pre>
<p>Optional: For multi-line commands you can specify the <code class="language-markup">data-continuation-str</code> attribute on the <code class="language-markup">&lt;pre></code> element. For example, <code class="language-markup">data-continuation-str="\"</code> will treat lines ending with <code class="language-markup">\</code> as being continued on the following line. Continued lines will have a prompt as set by the attribute <code class="language-markup">data-continuation-prompt</code> or a default of <code class="language-markup">&gt;</code>.</p>
<h2>Optional: Multi-line commands</h2>
<p>You can configure the plugin to handle multi-line commands. This can be done in two ways; setting a line continuation string (as in Bash); or explicitly marking continuation lines with a prefix for languages that do not have a continuation string/character, e.g. SQL, Scala, etc..</p>
<dl>
<dt><code class="language-markup">data-continuation-str</code></dt>
<dd>Set this attribute to the line continuation string/character, e.g. for bash <code class="language-markup">data-continuation-str="\"</code></dd>
<dt><code class="language-markup">data-filter-continuation</code></dt>
<dd>This works in a similar way to <code class="language-markup">data-filter-output</code>. Prefix all continuation lines with the value of <code class="language-markup">data-filter-continuation</code> and they will be displayed with the prompt set in <code class="language-markup">data-continuation-prompt</code>. For example, <code class="language-markup">data-filter-continuation="(con)"</code> will treat lines beginning with <code class="language-markup">(con)</code> as continuation lines and remove the prefix.</dd>
<dt><code class="language-markup">data-continuation-prompt</code></dt>
<dd>Set this attribute to define the prompt to be displayed when the command has continued beyond the first line (whether using line continuation or command termination), e.g. for MySQL <code class="language-markup">data-continuation-prompt="-&gt;"</code>. If this attribute is not set then a default of <code class="language-markup">&gt;</code> will be used.</dd>
</dl>
</section>
<section>
<h1>Examples</h1>
<h2>Default Use Without Output</h2>
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;&gt;</code></pre>
<pre class="command-line"><code class="language-bash">cd ~/.vim
vim vimrc</code></pre>
<h2>Root User Without Output</h2>
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
data-user=&quot;root&quot;
data-host=&quot;localhost&quot;&gt;</code></pre>
<pre class="command-line" data-user="root" data-host="localhost"><code class="language-bash">cd /usr/local/etc
cp php.ini php.ini.bak
vi php.ini</code></pre>
<h2>Non-Root User With Output</h2>
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
data-user=&quot;chris&quot;
data-host=&quot;remotehost&quot;
data-output=&quot;2, 4-8&quot;&gt;</code></pre>
<pre class="command-line" data-user="chris" data-host="remotehost" data-output="2, 4-8"><code class="language-bash">pwd
/usr/home/chris/bin
ls -la
@ -78,6 +113,11 @@ drwxr--r-x 45 chris chris 92 Feb 14 11:10 ..
-rwxr-xr-x 1 chris chris 642 Jan 17 14:42 deploy</code></pre>
<h2>Windows PowerShell With Output</h2>
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
data-prompt=&quot;PS C:\Users\Chris&gt;&quot;
data-output=&quot;2-19&quot;&gt;</code></pre>
<pre class="command-line" data-prompt="PS C:\Users\Chris>" data-output="2-19"><code class="language-powershell">dir
@ -99,7 +139,14 @@ d-r-- 10/14/2015 5:06 PM Searches
d-r-- 10/14/2015 5:06 PM Videos</code></pre>
<h2>Line continuation with Output (bash)</h2>
<pre class="command-line" data-filter-output="(out)" data-continuation-str="\" ><code class="language-bash">echo "hello"
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
data-filter-output=&quot;(out)&quot;
data-continuation-str=&quot;\&quot; &gt;</code></pre>
<pre class="command-line" data-filter-output="(out)" data-continuation-str="\" ><code class="language-bash">export MY_VAR=123
echo "hello"
(out)hello
echo one \
two \
@ -110,7 +157,14 @@ echo "goodbye"
(out)goodbye</code></pre>
<h2>Line continuation with Output (PowerShell)</h2>
<pre class="command-line" data-prompt="PS C:\Users\Chris>" data-continuation-prompt=">>" data-filter-output="(out)" data-continuation-str=" `"><code class="language-powershell">Write-Host `
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
data-prompt=&quot;ps c:\users\chris&gt;&quot;
data-continuation-prompt=&quot;&gt;&gt;&quot;
data-filter-output=&quot;(out)&quot;
data-continuation-str=&quot; `&quot;&gt;</code></pre>
<pre class="command-line" data-prompt="ps c:\users\chris>" data-continuation-prompt=">>" data-filter-output="(out)" data-continuation-str=" `"><code class="language-powershell">Write-Host `
'Hello' `
'from' `
'PowerShell!'
@ -118,6 +172,37 @@ echo "goodbye"
Write-Host 'Goodbye from PowerShell!'
(out)Goodbye from PowerShell!</code></pre>
<h2>Line continuation using prefix (MySQL/SQL)</h2>
<pre><code class="language-html">&lt;pre class=&quot;command-line&quot;
data-prompt=&quot;mysql&gt;&quot;
data-continuation-prompt=&quot;-&gt;&quot;
data-filter-output=&quot;(out)&quot;
data-filter-continuation=&quot;(con)&quot;&gt;</code></pre>
<pre class="command-line" data-prompt="mysql>" data-continuation-prompt="->" data-filter-output="(out)" data-filter-continuation="(con)"><code class="language-sql">set @my_var = 'foo';
set @my_other_var = 'bar';
(out)
CREATE TABLE people (
(con)first_name VARCHAR(30) NOT NULL,
(con)last_name VARCHAR(30) NOT NULL
(con));
(out)Query OK, 0 rows affected (0.09 sec)
(out)
insert into people
(con)values ('John', 'Doe');
(out)Query OK, 1 row affected (0.02 sec)
(out)
select *
(con)from people
(con)order by last_name;
(out)+------------+-----------+
(out)| first_name | last_name |
(out)+------------+-----------+
(out)| John | Doe |
(out)+------------+-----------+
(out)1 row in set (0.00 sec)</code></pre>
</section>
<footer data-src="assets/templates/footer.html" data-type="text/html"></footer>
@ -129,6 +214,7 @@ Write-Host 'Goodbye from PowerShell!'
<script src="assets/code.js"></script>
<script src="components/prism-bash.js"></script>
<script src="components/prism-powershell.js"></script>
<script src="components/prism-sql.js"></script>
</body>
</html>

View File

@ -74,21 +74,6 @@
var codeLines = env.code.split('\n');
var continuationLineIndicies = commandLine.continuationLineIndicies = new Set();
var lineContinuationStr = pre.getAttribute('data-continuation-str');
// Identify code lines that are a continuation line and thus don't need
// a prompt
if (lineContinuationStr && codeLines.length > 1) {
for (var j = 1; j < codeLines.length; j++) {
if (codeLines.hasOwnProperty(j - 1)
&& endsWith(codeLines[j - 1], lineContinuationStr)) {
// Mark this line as being a continuation line
continuationLineIndicies.add(j);
}
}
}
commandLine.numberOfLines = codeLines.length;
/** @type {string[]} */
var outputLines = commandLine.outputLines = [];
@ -127,6 +112,32 @@
}
}
var continuationLineIndicies = commandLine.continuationLineIndicies = new Set();
var lineContinuationStr = pre.getAttribute('data-continuation-str');
var continuationFilter = pre.getAttribute('data-filter-continuation');
// Identify code lines where the command has continued onto subsequent
// lines and thus need a different prompt. Need to do this after the output
// lines have been removed to ensure we don't pick up a continuation string
// in an output line.
for (var j = 0; j < codeLines.length; j++) {
var line = codeLines[j];
if (!line) {
continue;
}
// Record the next line as a continuation if this one ends in a continuation str.
if (lineContinuationStr && endsWith(line, lineContinuationStr)) {
continuationLineIndicies.add(j + 1);
}
// Record this line as a continuation if marked with a continuation prefix
// (that we will remove).
if (j > 0 && continuationFilter && startsWith(line, continuationFilter)) {
codeLines[j] = line.slice(continuationFilter.length);
continuationLineIndicies.add(j);
}
}
env.code = codeLines.join('\n');
});

View File

@ -1 +1 @@
!function(){if("undefined"!=typeof Prism&&"undefined"!=typeof document){var v=/(?:^|\s)command-line(?:\s|$)/,g="command-line-prompt",p="".startsWith?function(e,t){return e.startsWith(t)}:function(e,t){return 0===e.indexOf(t)},d="".endsWith?function(e,t){return e.endsWith(t)}:function(e,t){var n=e.length;return e.substring(n-t.length,n)===t};Prism.hooks.add("before-highlight",function(e){var t=N(e);if(!t.complete&&e.code){var n=e.element.parentElement;if(n&&/pre/i.test(n.nodeName)&&(v.test(n.className)||v.test(e.element.className))){var a=e.element.querySelector("."+g);a&&a.remove();var i=e.code.split("\n"),r=t.continuationLineIndicies=new Set,s=n.getAttribute("data-continuation-str");if(s&&1<i.length)for(var o=1;o<i.length;o++)i.hasOwnProperty(o-1)&&d(i[o-1],s)&&r.add(o);t.numberOfLines=i.length;var l=t.outputLines=[],m=n.getAttribute("data-output"),u=n.getAttribute("data-filter-output");if(null!==m)m.split(",").forEach(function(e){var t=e.split("-"),n=parseInt(t[0],10),a=2===t.length?parseInt(t[1],10):n;if(!isNaN(n)&&!isNaN(a)){n<1&&(n=1),a>i.length&&(a=i.length),a--;for(var r=--n;r<=a;r++)l[r]=i[r],i[r]=""}});else if(u)for(var c=0;c<i.length;c++)p(i[c],u)&&(l[c]=i[c].slice(u.length),i[c]="");e.code=i.join("\n")}else t.complete=!0}else t.complete=!0}),Prism.hooks.add("before-insert",function(e){var t=N(e);if(!t.complete){for(var n=e.highlightedCode.split("\n"),a=t.outputLines||[],r=0,i=n.length;r<i;r++)a.hasOwnProperty(r)?n[r]='<span class="token output">'+Prism.util.encode(a[r])+"</span>":n[r]='<span class="token command">'+n[r]+"</span>";e.highlightedCode=n.join("\n")}}),Prism.hooks.add("complete",function(e){if(function(e){return"command-line"in(e.vars=e.vars||{})}(e)){var t=N(e);if(!t.complete){var n=e.element.parentElement;v.test(e.element.className)&&(e.element.className=e.element.className.replace(v," ")),v.test(n.className)||(n.className+=" command-line");var a,r="",i=t.numberOfLines||0,s=h("data-prompt","");if(""!==s)a='<span data-prompt="'+s+'"></span>';else a='<span data-user="'+h("data-user","user")+'" data-host="'+h("data-host","localhost")+'"></span>';for(var o=t.continuationLineIndicies||new Set,l='<span data-continuation-prompt="'+h("data-continuation-prompt",">")+'"></span>',m=0;m<i;m++)o.has(m)?r+=l:r+=a;var u=document.createElement("span");u.className=g,u.innerHTML=r;for(var c=t.outputLines||[],p=0,d=c.length;p<d;p++)if(c.hasOwnProperty(p)){var f=u.children[p];f.removeAttribute("data-user"),f.removeAttribute("data-host"),f.removeAttribute("data-prompt")}e.element.insertBefore(u,e.element.firstChild),t.complete=!0}}function h(e,t){return(n.getAttribute(e)||t).replace(/"/g,"&quot")}})}function N(e){var t=e.vars=e.vars||{};return t["command-line"]=t["command-line"]||{}}}();
!function(){if("undefined"!=typeof Prism&&"undefined"!=typeof document){var v=/(?:^|\s)command-line(?:\s|$)/,g="command-line-prompt",f="".startsWith?function(e,t){return e.startsWith(t)}:function(e,t){return 0===e.indexOf(t)},h="".endsWith?function(e,t){return e.endsWith(t)}:function(e,t){var n=e.length;return e.substring(n-t.length,n)===t};Prism.hooks.add("before-highlight",function(e){var t=b(e);if(!t.complete&&e.code){var n=e.element.parentElement;if(n&&/pre/i.test(n.nodeName)&&(v.test(n.className)||v.test(e.element.className))){var a=e.element.querySelector("."+g);a&&a.remove();var i=e.code.split("\n");t.numberOfLines=i.length;var o=t.outputLines=[],r=n.getAttribute("data-output"),s=n.getAttribute("data-filter-output");if(null!==r)r.split(",").forEach(function(e){var t=e.split("-"),n=parseInt(t[0],10),a=2===t.length?parseInt(t[1],10):n;if(!isNaN(n)&&!isNaN(a)){n<1&&(n=1),a>i.length&&(a=i.length),a--;for(var r=--n;r<=a;r++)o[r]=i[r],i[r]=""}});else if(s)for(var l=0;l<i.length;l++)f(i[l],s)&&(o[l]=i[l].slice(s.length),i[l]="");for(var m=t.continuationLineIndicies=new Set,u=n.getAttribute("data-continuation-str"),c=n.getAttribute("data-filter-continuation"),d=0;d<i.length;d++){var p=i[d];p&&(u&&h(p,u)&&m.add(d+1),0<d&&c&&f(p,c)&&(i[d]=p.slice(c.length),m.add(d)))}e.code=i.join("\n")}else t.complete=!0}else t.complete=!0}),Prism.hooks.add("before-insert",function(e){var t=b(e);if(!t.complete){for(var n=e.highlightedCode.split("\n"),a=t.outputLines||[],r=0,i=n.length;r<i;r++)a.hasOwnProperty(r)?n[r]='<span class="token output">'+Prism.util.encode(a[r])+"</span>":n[r]='<span class="token command">'+n[r]+"</span>";e.highlightedCode=n.join("\n")}}),Prism.hooks.add("complete",function(e){if(function(e){return"command-line"in(e.vars=e.vars||{})}(e)){var t=b(e);if(!t.complete){var n=e.element.parentElement;v.test(e.element.className)&&(e.element.className=e.element.className.replace(v," ")),v.test(n.className)||(n.className+=" command-line");var a,r="",i=t.numberOfLines||0,o=h("data-prompt","");if(""!==o)a='<span data-prompt="'+o+'"></span>';else a='<span data-user="'+h("data-user","user")+'" data-host="'+h("data-host","localhost")+'"></span>';for(var s=t.continuationLineIndicies||new Set,l='<span data-continuation-prompt="'+h("data-continuation-prompt",">")+'"></span>',m=0;m<i;m++)s.has(m)?r+=l:r+=a;var u=document.createElement("span");u.className=g,u.innerHTML=r;for(var c=t.outputLines||[],d=0,p=c.length;d<p;d++)if(c.hasOwnProperty(d)){var f=u.children[d];f.removeAttribute("data-user"),f.removeAttribute("data-host"),f.removeAttribute("data-prompt")}e.element.insertBefore(u,e.element.firstChild),t.complete=!0}}function h(e,t){return(n.getAttribute(e)||t).replace(/"/g,"&quot")}})}function b(e){var t=e.vars=e.vars||{};return t["command-line"]=t["command-line"]||{}}}();