User:Riviera/Go fish

From XPUB & Lens-Based wiki
< User:Riviera
Revision as of 16:41, 1 October 2023 by Riviera (talk | contribs) (Created page with "<blockquote> In the shell I find a marvelous mess of constellations, nebulae, interstellar gaps, awesome gullies, that provokes in me an indescribable sense of vertigo, as if I am hanging from earth upside down on the brink of infinite space, with terrestrial gravity still holding me by the heels but about to release me any moment. ---- Nancy Mauro-Flude, 2008 </blockquote> =A Two-Part Syntax Highlighter= Below are two fish scripts. The first function, <tt>highlight</...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In the shell I find a marvelous mess of constellations, nebulae, interstellar gaps, awesome gullies, that provokes in me an indescribable sense of vertigo, as if I am hanging from earth upside down on the brink of infinite space, with terrestrial gravity still holding me by the heels but about to release me any moment.


Nancy Mauro-Flude, 2008


A Two-Part Syntax Highlighter

Below are two fish scripts. The first function, highlight, utilises the sed command to edit HTML snippets generated by Emacs' Org Mode's HTML export backend. The second script works by calling the highlight function several times on the same file.

higlight.fish

#! /usr/bin/fish

function highlight -d "Highlight HTML code" -a file

   argparse --name='highlight' 'h/help' -- $argv;
   sed -i 's/<pre/<div/' $argv;
   sed -i 's/<\/pre>/<\/div>/' $argv;
   sed -i 's/style=/style=/' $argv;
   sed -i -E 's/"src src-[[:alpha:]]*"/"font-family: Monospace; background-color: #dbe6f0;"/' $argv;
   sed -i 's/"color:teal"/"color:teal"/' $argv;
   sed -i 's/"color:teal"/"color:teal"/' $argv;
   sed -i 's/"color:green"/"color:green"/' $argv;
   sed -i 's/"color:yellow"/"color:yellow"/' $argv;
   sed -i 's/"color:orange"/"color:orange"/' $argv;
   sed -i 's/"color:olive"/"color:olive"/' $argv;
   sed -i 's/"color:gray"/"color:gray"/' $argv;
   sed -i 's/"color:purple"/"color:purple"/' $argv;

end

The s command in sed language is a shorthand for substitute. s commands are of the form

's/find/replace/'

With the s command, sed queries a text file for the presence of a particular string, or regular expression, and replaces what is found with user defined text. In the above example, classes of HTML elements are restyled with colour to highlight different parts of the script.

higlight_all.fish

#! /usr/bin/fish

function highlight_all -d "Call the highlight command several times" -a file

   set -l options (fish_opt -s h -l help)
   argparse --name='highlight_all' 'h/help' -- $argv;
   for n in (seq 1 5);
       highlight $argv;
   end

end