💾 Archived View for midnight.pub › replies › 41 captured on 2022-04-28 at 20:28:11. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2024-08-18)

-=-=-=-=-=-=-

< Script fiddling

Parent

~inquiry

This is to see of code/pre tags work in Midnight Pub posts.

So here are the two pieces of code whose high-level description (when combined) transforms text into Mignight Pub friendly text/tags (huh... but I see I used two Lua scripts - not one Lua and one bash as first reported);

The "y1" script:

<pre><code>#! /usr/bin/env lua<br>local lines = {}<br>for line in io.stdin:lines() do<br>table.insert(lines, line)<br>end<br>local url = ''<br>if arg[1] then<br>url = ' ' .. arg[1]<br>end<br>local handle = io.popen('fmt -60 | quote-br' .. url, 'w')<br>handle:write(table.concat(lines, ' '))<br>handle:close()<br>local num_url_spaces = 30<br>if arg[1] then<br>io.stdout:write('><br>>')<br>for i=1,num_url_spaces do<br>io.stdout:write('&nbsp;')<br>end<br>print('- ' .. arg[1])<br>end<br></code></pre>

The "quote-br" script called by the "y1" script:

<pre><code>#! /usr/bin/env lua<br>local lines = {}<br>for line in io.stdin:lines() do<br>table.insert(lines, '> ' .. line .. '<br>')<br>end<br>io.stdout:write(table.concat(lines, ''))<br></code></pre>

Write a reply

Replies

~inquiry wrote:

Okay, trying it one more time, this time with a bit more attention to lines beginning with whitespace:

y1:

<pre><code>#! /usr/bin/env lua<br>local lines = {}<br>for line in io.stdin:lines() do<br>&nbsp;&nbsp;table.insert(lines, line)<br>end<br>local url = ''<br>if arg[1] then<br>&nbsp;&nbsp;url = ' ' .. arg[1]<br>end<br>local handle = io.popen('fmt -60 | quote-br' .. url, 'w')<br>handle:write(table.concat(lines, ' '))<br>handle:close()<br>local num_url_spaces = 30<br>if arg[1] then<br>&nbsp;&nbsp;io.stdout:write('&gt;&lt;br&gt;&gt;')<br>&nbsp;&nbsp;for i=1,num_url_spaces do<br>&nbsp;&nbsp;&nbsp;&nbsp;io.stdout:write('&nbsp;')<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;print('- ' .. arg[1])<br>end<br></code></pre>

quote-br:

<pre><code>#! /usr/bin/env lua<br>local lines = {}<br>for line in io.stdin:lines() do<br>&nbsp;&nbsp;if arg[1] then<br>&nbsp;&nbsp;&nbsp;&nbsp;local whitespace,rest = string.match(line, '^(%s+)(.*) )<br>&nbsp;&nbsp;&nbsp;&nbsp;if whitespace then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whitespace = string.gsub(whitespace, ' ', '&nbsp;&nbsp;')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;whitespace = string.gsub(whitespace, '\t', '&nbsp;&nbsp;')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if rest then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rest = string.gsub(rest, '&lt;', '&lt;')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rest = string.gsub(rest, '&gt;', '&gt;')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rest = ''<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line = whitespace .. rest<br>&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;if arg[1] then<br>&nbsp;&nbsp;&nbsp;&nbsp;table.insert(lines, line .. '&lt;br&gt;')<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;table.insert(lines, '&gt; ' .. line .. '&lt;br&gt;')<br>&nbsp;&nbsp;end<br>end<br>io.stdout:write(table.concat(lines, ''))<br></code></pre>

~inquiry wrote (thread):

Hmmm.

So I needed to add &lt;br&gt; tags to the end of all the code lines, then much all the code lines into a single line to make it work. Not too convenient.

And then I'd need to go a step further and convert tab characters (which I prefer for indentation) to good 'ole "nbsp" entities....

&lt;gives up&gt;