02 October 2016

HTML entities helper

While I was building my custom keys page I realised I needed to use HTML entities to avoid some characters that break the markdown to HTML process. In particular backquote/grave accent, used by markdown to indicate code & the pipe/vertical line, which is used to build tables.

By the way, this is something I just built incidentally. Maybe I should turn it into a package, but for now I thought I’d just share how I built it.

First off I need a list of HTML entities, I used the one on Wikipedia.

On that page, there’s a single table.wikitable.sortable . This contains all the useful HTML entity info.

From the Chrome Dev Tools console, I used a little jQuery and JS/ES6, and grabbed the useful text from the table of entities. Here’s the code I used. (reformatted from a one-liner, comments added)

$ ( 'table.wikitable.sortable tr' ). map (( i , e ) => { // Pick cell 0,1,6 (entity, char and description) // from each table row [ ent , chr ,,,,, dsc ] = $ ( e . children ). toArray () . map (( e , i ) => e . innerText ) return ` char : $ { chr } entity : & $ { ent }; text : $ { dsc } ` }). toArray (). join ( "

" );

The output looks like this:

char: ¢ entity: ¢ text: cent sign char: £ entity: £ text: pound sign char: ¤ entity: ¤ text: currency sign char: ¥ entity: ¥ text: yen sign (yuan sign)

While I could use one of many scripting options to do this scraping, I find that using the Dev Tools to do an initial scrape and figure out CSS selector paths, the interactivity, also gives us faster feedback.

If I’m going to scrape the same page many times, and I expect more dynamic content, I’d definitely migrate out to something else. (What that is would depend on a lot of factors.)

Anyway, now that I have my list, I’ll paste it into Emacs and let’s do a little tidying up. (I could’ve done most of this during the scrape BTW.)

After trimming off the enclosing quotes, I convert each line into a lisp string. I’ll do a query-regexp-replace from the top of the buffer: C-M-% regexp: .* with: "\&" (then ! replace all)

"char: ¢ entity: ¢ text: cent sign" "char: £ entity: £ text: pound sign" "char: ¤ entity: ¤ text: currency sign" "char: ¥ entity: ¥ text: yen sign (yuan sign)"

I’ll align the text: entries so that later on the selection process will feel nicer.

I select a region with all the entries and do M-x align-regexp with: “ text: ”. We get this:

"char: ¢ entity: ¢ text: cent sign" "char: £ entity: £ text: pound sign" "char: ¤ entity: ¤ text: currency sign" "char: ¥ entity: ¥ text: yen sign (yuan sign)"

Next wrap the whole thing in defvar as a list:

( defvar html-entity-list ' ( "char: \" entity: " text: quotation mark (APL quote)" "char: & entity: & text: ampersand" "char: ' entity: ' text: apostrophe (apostrophe-quote); see below" "char: < entity: < text: less-than sign" "char: > entity: > text: greater-than sign" "char: entity: text: no-break space (non-breaking space)[d]" "char: ¡ entity: ¡ text: inverted exclamation mark" "char: ¢ entity: ¢ text: cent sign" "char: £ entity: £ text: pound sign" "char: ¤ entity: ¤ text: currency sign" "char: ¥ entity: ¥ text: yen sign (yuan sign)" "char: ¦ entity: ¦ text: broken bar (broken vertical bar)" "char: § entity: § text: section sign" "char: | entity: | text: vertical bar (unix pipe)" "char: ¨ entity: ¨ text: diaeresis (spacing diaeresis); see Germanic umlaut" "char: © entity: © text: copyright symbol" "char: ª entity: ª text: feminine ordinal indicator" "char: « entity: « text: left-pointing double angle quotation mark (left pointing guillemet)" "char: ¬ entity: ¬ text: not sign" "char: entity: ­ text: soft hyphen (discretionary hyphen)" "char: ® entity: ® text: registered sign (registered trademark symbol)" "char: ¯ entity: ¯ text: macron (spacing macron, overline, APL overbar)" "char: ° entity: ° text: degree symbol" "char: ± entity: ± text: plus-minus sign (plus-or-minus sign)" "char: ² entity: ² text: superscript two (superscript digit two, squared)" "char: ³ entity: ³ text: superscript three (superscript digit three, cubed)" "char: ´ entity: ´ text: acute accent (spacing acute)" "char: µ entity: µ text: micro sign" "char: ¶ entity: ¶ text: pilcrow sign (paragraph sign)" "char: · entity: · text: middle dot (Georgian comma, Greek middle dot)" "char: ¸ entity: ¸ text: cedilla (spacing cedilla)" "char: ¹ entity: ¹ text: superscript one (superscript digit one)" "char: º entity: º text: masculine ordinal indicator" "char: » entity: » text: right-pointing double angle quotation mark (right pointing guillemet)" "char: ¼ entity: ¼ text: vulgar fraction one quarter (fraction one quarter)" "char: ½ entity: ½ text: vulgar fraction one half (fraction one half)" "char: ¾ entity: ¾ text: vulgar fraction three quarters (fraction three quarters)" "char: ¿ entity: ¿ text: inverted question mark (turned question mark)" "char: ` entity: ` text: grave accent" "char: À entity: À text: Latin capital letter A with grave accent (Latin capital letter A grave)" "char: Á entity: Á text: Latin capital letter A with acute accent" "char: Â entity: Â text: Latin capital letter A with circumflex" "char: Ã entity: Ã text: Latin capital letter A with tilde" "char: Ä entity: Ä text: Latin capital letter A with diaeresis" "char: Å entity: Å text: Latin capital letter A with ring above (Latin capital letter A ring)" "char: Æ entity: Æ text: Latin capital letter AE (Latin capital ligature AE)" "char: Ç entity: Ç text: Latin capital letter C with cedilla" "char: È entity: È text: Latin capital letter E with grave accent" "char: É entity: É text: Latin capital letter E with acute accent" "char: Ê entity: Ê text: Latin capital letter E with circumflex" "char: Ë entity: Ë text: Latin capital letter E with diaeresis" "char: Ì entity: Ì text: Latin capital letter I with grave accent" "char: Í entity: Í text: Latin capital letter I with acute accent" "char: Î entity: Î text: Latin capital letter I with circumflex" "char: Ï entity: Ï text: Latin capital letter I with diaeresis" "char: Ð entity: Ð text: Latin capital letter Eth" "char: Ñ entity: Ñ text: Latin capital letter N with tilde" "char: Ò entity: Ò text: Latin capital letter O with grave accent" "char: Ó entity: Ó text: Latin capital letter O with acute accent" "char: Ô entity: Ô text: Latin capital letter O with circumflex" "char: Õ entity: Õ text: Latin capital letter O with tilde" "char: Ö entity: Ö text: Latin capital letter O with diaeresis" "char: × entity: × text: multiplication sign" "char: Ø entity: Ø text: Latin capital letter O with stroke (Latin capital letter O slash)" "char: Ù entity: Ù text: Latin capital letter U with grave accent" "char: Ú entity: Ú text: Latin capital letter U with acute accent" "char: Û entity: Û text: Latin capital letter U with circumflex" "char: Ü entity: Ü text: Latin capital letter U with diaeresis" "char: Ý entity: Ý text: Latin capital letter Y with acute accent" "char: Þ entity: Þ text: Latin capital letter THORN" "char: ß entity: ß text: Latin small letter sharp s (ess-zed); see German Eszett" "char: à entity: à text: Latin small letter a with grave accent" "char: á entity: á text: Latin small letter a with acute accent" "char: â entity: â text: Latin small letter a with circumflex" "char: ã entity: ã text: Latin small letter a with tilde" "char: ä entity: ä text: Latin small letter a with diaeresis" "char: å entity: å text: Latin small letter a with ring above" "char: æ entity: æ text: Latin small letter ae (Latin small ligature ae)" "char: ç entity: ç text: Latin small letter c with cedilla" "char: è entity: è text: Latin small letter e with grave accent" "char: é entity: é text: Latin small letter e with acute accent" "char: ê entity: ê text: Latin small letter e with circumflex" "char: ë entity: ë text: Latin small letter e with diaeresis" "char: ì entity: ì text: Latin small letter i with grave accent" "char: í entity: í text: Latin small letter i with acute accent" "char: î entity: î text: Latin small letter i with circumflex" "char: ï entity: ï text: Latin small letter i with diaeresis" "char: ð entity: ð text: Latin small letter eth" "char: ñ entity: ñ text: Latin small letter n with tilde" "char: ò entity: ò text: Latin small letter o with grave accent" "char: ó entity: ó text: Latin small letter o with acute accent" "char: ô entity: ô text: Latin small letter o with circumflex" "char: õ entity: õ text: Latin small letter o with tilde" "char: ö entity: ö text: Latin small letter o with diaeresis" "char: ÷ entity: ÷ text: division sign (obelus)" "char: ø entity: ø text: Latin small letter o with stroke (Latin small letter o slash)" "char: ù entity: ù text: Latin small letter u with grave accent" "char: ú entity: ú text: Latin small letter u with acute accent" "char: û entity: û text: Latin small letter u with circumflex" "char: ü entity: ü text: Latin small letter u with diaeresis" "char: ý entity: ý text: Latin small letter y with acute accent" "char: þ entity: þ text: Latin small letter thorn" "char: ÿ entity: ÿ text: Latin small letter y with diaeresis" "char: Œ entity: Œ text: Latin capital ligature oe[e]" "char: œ entity: œ text: Latin small ligature oe[e]" "char: Š entity: Š text: Latin capital letter s with caron" "char: š entity: š text: Latin small letter s with caron" "char: Ÿ entity: Ÿ text: Latin capital letter y with diaeresis" "char: ƒ entity: ƒ text: Latin small letter f with hook (function, florin)" "char: ˆ entity: ˆ text: modifier letter circumflex accent" "char: ˜ entity: ˜ text: small tilde" "char: Α entity: Α text: Greek capital letter Alpha" "char: Β entity: Β text: Greek capital letter Beta" "char: Γ entity: Γ text: Greek capital letter Gamma" "char: Δ entity: Δ text: Greek capital letter Delta" "char: Ε entity: Ε text: Greek capital letter Epsilon" "char: Ζ entity: Ζ text: Greek capital letter Zeta" "char: Η entity: Η text: Greek capital letter Eta" "char: Θ entity: Θ text: Greek capital letter Theta" "char: Ι entity: Ι text: Greek capital letter Iota" "char: Κ entity: Κ text: Greek capital letter Kappa" "char: Λ entity: Λ text: Greek capital letter Lambda" "char: Μ entity: Μ text: Greek capital letter Mu" "char: Ν entity: Ν text: Greek capital letter Nu" "char: Ξ entity: Ξ text: Greek capital letter Xi" "char: Ο entity: Ο text: Greek capital letter Omicron" "char: Π entity: Π text: Greek capital letter Pi" "char: Ρ entity: Ρ text: Greek capital letter Rho" "char: Σ entity: Σ text: Greek capital letter Sigma" "char: Τ entity: Τ text: Greek capital letter Tau" "char: Υ entity: Υ text: Greek capital letter Upsilon" "char: Φ entity: Φ text: Greek capital letter Phi" "char: Χ entity: Χ text: Greek capital letter Chi" "char: Ψ entity: Ψ text: Greek capital letter Psi" "char: Ω entity: Ω text: Greek capital letter Omega" "char: α entity: α text: Greek small letter alpha" "char: β entity: β text: Greek small letter beta" "char: γ entity: γ text: Greek small letter gamma" "char: δ entity: δ text: Greek small letter delta" "char: ε entity: ε text: Greek small letter epsilon" "char: ζ entity: ζ text: Greek small letter zeta" "char: η entity: η text: Greek small letter eta" "char: θ entity: θ text: Greek small letter theta" "char: ι entity: ι text: Greek small letter iota" "char: κ entity: κ text: Greek small letter kappa" "char: λ entity: λ text: Greek small letter lambda" "char: μ entity: μ text: Greek small letter mu" "char: ν entity: ν text: Greek small letter nu" "char: ξ entity: ξ text: Greek small letter xi" "char: ο entity: ο text: Greek small letter omicron" "char: π entity: π text: Greek small letter pi" "char: ρ entity: ρ text: Greek small letter rho" "char: ς entity: ς text: Greek small letter final sigma" "char: σ entity: σ text: Greek small letter sigma" "char: τ entity: τ text: Greek small letter tau" "char: υ entity: υ text: Greek small letter upsilon" "char: φ entity: φ text: Greek small letter phi" "char: χ entity: χ text: Greek small letter chi" "char: ψ entity: ψ text: Greek small letter psi" "char: ω entity: ω text: Greek small letter omega" "char: ϑ entity: ϑ text: Greek theta symbol" "char: ϒ entity: ϒ text: Greek Upsilon with hook symbol" "char: ϖ entity: ϖ text: Greek pi symbol" "char: entity: text: en space[d]" "char: entity: text: em space[d]" "char: entity: text: thin space[d]" "char: entity: ‌ text: zero-width non-joiner" "char: entity: ‍ text: zero-width joiner" "char: entity: ‎ text: left-to-right mark" "char: entity: ‏ text: right-to-left mark" "char: – entity: – text: en dash" "char: — entity: — text: em dash" "char: ‘ entity: ‘ text: left single quotation mark" "char: ’ entity: ’ text: right single quotation mark" "char: ‚ entity: ‚ text: single low-9 quotation mark" "char: “ entity: “ text: left double quotation mark" "char: ” entity: ” text: right double quotation mark" "char: „ entity: „ text: double low-9 quotation mark" "char: † entity: † text: dagger, obelisk" "char: ‡ entity: ‡ text: double dagger, double obelisk" "char: • entity: • text: bullet (black small circle)[f]" "char: … entity: … text: horizontal ellipsis (three dot leader)" "char: ‰ entity: ‰ text: per mille sign" "char: ′ entity: ′ text: prime (minutes, feet)" "char: ″ entity: ″ text: double prime (seconds, inches)" "char: ‹ entity: ‹ text: single left-pointing angle quotation mark[g]" "char: › entity: › text: single right-pointing angle quotation mark[g]" "char: ‾ entity: ‾ text: overline (spacing overscore)" "char: ⁄ entity: ⁄ text: fraction slash (solidus)" "char: € entity: € text: euro sign" "char: ℑ entity: ℑ text: black-letter capital I (imaginary part)" "char: ℘ entity: ℘ text: script capital P (power set, Weierstrass p)" "char: ℜ entity: ℜ text: black-letter capital R (real part symbol)" "char: ™ entity: ™ text: trademark symbol" "char: ℵ entity: ℵ text: alef symbol (first transfinite cardinal)[h]" "char: ← entity: ← text: leftwards arrow" "char: ↑ entity: ↑ text: upwards arrow" "char: → entity: → text: rightwards arrow" "char: ↓ entity: ↓ text: downwards arrow" "char: ↔ entity: ↔ text: left right arrow" "char: ↵ entity: ↵ text: downwards arrow with corner leftwards (carriage return)" "char: ⇐ entity: ⇐ text: leftwards double arrow[i]" "char: ⇑ entity: ⇑ text: upwards double arrow" "char: ⇒ entity: ⇒ text: rightwards double arrow[j]" "char: ⇓ entity: ⇓ text: downwards double arrow" "char: ⇔ entity: ⇔ text: left right double arrow" "char: ∀ entity: ∀ text: for all" "char: ∂ entity: ∂ text: partial differential" "char: ∃ entity: ∃ text: there exists" "char: ∅ entity: ∅ text: empty set (null set); see also U+8960, ⌀" "char: ∇ entity: ∇ text: del or nabla (vector differential operator)" "char: ∈ entity: ∈ text: element of" "char: ∉ entity: ∉ text: not an element of" "char: ∋ entity: ∋ text: contains as member" "char: ∏ entity: ∏ text: n-ary product (product sign)[k]" "char: ∑ entity: ∑ text: n-ary summation[l]" "char: − entity: − text: minus sign" "char: ∗ entity: ∗ text: asterisk operator" "char: √ entity: √ text: square root (radical sign)" "char: ∝ entity: ∝ text: proportional to" "char: ∞ entity: ∞ text: infinity" "char: ∠ entity: ∠ text: angle" "char: ∧ entity: ∧ text: logical and (wedge)" "char: ∨ entity: ∨ text: logical or (vee)" "char: ∩ entity: ∩ text: intersection (cap)" "char: ∪ entity: ∪ text: union (cup)" "char: ∫ entity: ∫ text: integral" "char: ∴ entity: ∴ text: therefore sign" "char: ∼ entity: ∼ text: tilde operator (varies with, similar to)[m]" "char: ≅ entity: ≅ text: congruent to" "char: ≈ entity: ≈ text: almost equal to (asymptotic to)" "char: ≠ entity: ≠ text: not equal to" "char: ≡ entity: ≡ text: identical to; sometimes used for 'equivalent to'" "char: ≤ entity: ≤ text: less-than or equal to" "char: ≥ entity: ≥ text: greater-than or equal to" "char: ⊂ entity: ⊂ text: subset of" "char: ⊃ entity: ⊃ text: superset of[n]" "char: ⊄ entity: ⊄ text: not a subset of" "char: ⊆ entity: ⊆ text: subset of or equal to" "char: ⊇ entity: ⊇ text: superset of or equal to" "char: ⊕ entity: ⊕ text: circled plus (direct sum)" "char: ⊗ entity: ⊗ text: circled times (vector product)" "char: ⊥ entity: ⊥ text: up tack (orthogonal to, perpendicular)[o]" "char: ⋅ entity: ⋅ text: dot operator[p]" "char: ⌈ entity: ⌈ text: left ceiling (APL upstile)" "char: ⌉ entity: ⌉ text: right ceiling" "char: ⌊ entity: ⌊ text: left floor (APL downstile)" "char: ⌋ entity: ⌋ text: right floor" "char: 〈 entity: ⟨ text: left-pointing angle bracket (bra)[q]" "char: 〉 entity: ⟩ text: right-pointing angle bracket (ket)[r]" "char: ◊ entity: ◊ text: lozenge" "char: ♠ entity: ♠ text: black spade suit[f]" "char: ♣ entity: ♣ text: black club suit (shamrock)[f]" "char: ♥ entity: ♥ text: black heart suit (valentine)[f]" "char: ♦ entity: ♦ text: black diamond suit[f]" ) "List of HTML entities with helpful text notes. Scraped from Wikipedia at https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references" )

Now, using completing-read we can select a row from the list with completion:

( defun html-entity-select () "Select an html entry from the list." ( interactive ) ( completing-read "HTML Entity: " html-entity-list ))

With completing-read we can select from the list and get Emacs built in completion.

This is ok, but the great thing about completing-read is it’s overriden by some neat selection frameworks. Ido ( ido-everywhere and ido-ubiquitous in particular) and Ivy ( ivy-completing-read ) both do (or allow) this approach. Since I use ido I get this selection style: (BTW I’m using ido-vertical for my aesthetic preferences and flx-ido to get fuzzy matching) )

Note: Helm is also a good candidate selection framework, but it needs specialized functions written for it.

Finally let’s tie everything together into a command we can bind to the keyboard:

( require 's ) ( defun html-entity-insert () "Select and insert an html entity." ( interactive ) ( let* (( entry ( html-entity-select )) ;; use a regexp to extract the HTML entity. ( entity ( second ( s-match "entity: \\(&.*?;\\)" entry )))) ( insert entity )))

We take the selected list entry and pull the entity string out of it. I use s.el for convenience. Then just use (insert ...) to enter it into our buffer.

I hope this was relatively simple to follow along, If you have any questions, tweet a message to @emacs_gifs.

The code for this post is on github at https://github.com/emacsfodder/html-entities-helper