https://nickdrozd.github.io/2022/12/19/wiktionary-advice.html
Something Something Programming [ ]
About
Some Advice for Browsing Wiktionary in Emacs
Dec 19, 2022
Emacs comes with a web browser built in. Although it's often regarded
as a novelty, EWW (as it's known) is a real browser, and I use it all
the time. A lot of web browsing involves text, and Emacs is the
ultimate text-handling platform, so it's obvioiusly a good match.
But as with many things in Emacs, EWW has some irregularities. For
example, here is the Wiktionary entry for the word "locus" as viewed
in EWW:
img
Five definitions are listed, but one of them is blank. What is the
missing definition? Is it important? I sure hope not. Dropping
information from a web page without warning would be a serious
problem!
Compare with the same page as viewed in Firefox:
img
The Firefox version shows only four definitions, exactly the same
ones shown in EWW. So it appears that rather than dropping a
definition, EWW is inserting a blank one. That's not such a serious
problem, but it's still kind of annoying.
To see what's going on, we need to look at the HTML source. (This can
be obtained from within EWW by pressing v.) The word definitions are
grouped in an ordered list ol with each individual definition showing
up as a list item li:
- A place or locality, especially a centre of activity or the scene of a crime.
The cafeteria was the locus of activity.
- (mathematics) The set of all points whose coordinates satisfy a given equation or condition.
A circle is the locus of points from which the distance to the center is a given value, the radius.
- (genetics) A fixed position on a chromosome that may be occupied by one or more genes.
- (chiefly in the plural) A passage in writing, especially in a collection of ancient sacred writings arranged according to a theme.
Apparently the blank definition comes from an explicitly empty list
item: . I don't know why Wiktionary
includes that list item, and I don't know how Firefox knows not to
display it.
It's not obvious that it's a bug to show the empty list item. I mean,
it's there, right? But still, it looks like an error, so I don't want
to see it. This will require modifying the browser's behavior.
Fortunately, Emacs provides a mechanism to unobtrusively modify any
behavior at all, namely advice. In short, we will define a new
function that will attach itself to an existing function and do some
extra stuff.
This could be done in lots of different ways. My idea was to modify
the list item handler shr-tag-li, a function of one argument:
(defun shr-tag-li (dom) ...)
We need to define a new function of two arguments to wrap this one.
The two arguments will be the handler function and its argument, and
the wrapping function will call the handler on its argument only when
the list item is non-empty:
(defun skip-empty-li (shrtagli dom)
(when (cddr dom)
(funcall shrtagli dom)))
The wrapper will be attached to the wrapped with advice-add:
(advice-add
'shr-tag-li :around
#'skip-empty-li)
And that's it. This fixes the problem:
img
Something Something Programming
* Something Something Programming
* nicholasdrozd@gmail.com
* nickdrozd
Mostly thoughts about programming. Maybe other stuff too.