devdocs.el icon indicating copy to clipboard operation
devdocs.el copied to clipboard

FIX: add a colon for `classifier` spans

Open jdtsmith opened this issue 1 year ago • 3 comments
trafficstars

It seems some docs (e.g. matplotlib) make heavy use of dl/dt with a classifier class set in a span inside the dt, e.g.:

<dt>
<strong>x</strong><span class="classifier">float, default: 0</span>
</dt>

In the devdocs css (at least for sphinx-derived docs), there is:

._python .classifier:before, ._sphinx .classifier:before {
content: ": ";
}

which magically adds a colon before the content. This is ignored by shr, resulting in:

image

This can be fixed by helping shr out:

(defun devdocs-tag-span (dom)
  (when-let ((attrs (nth 1 dom))
	     (class (alist-get 'class attrs))
	     ((member "classifier" (split-string class " "))))
    (shr-insert ": "))
  (shr-tag-span dom))

(setf (alist-get 'span shr-external-rendering-functions) #'devdocs-tag-span)

After:

image

This same technique could be used to patch any obvious deficiencies in the CSS styling (though this is the one I notice most).

jdtsmith avatar Jun 20 '24 15:06 jdtsmith

Thanks for the detailed info. This recent PR allows handling these kinds of situation. I will work on it, but probably not in the near future :-).

astoff avatar Jun 25 '24 10:06 astoff

Ok thanks. Do you plan pulling a collection of "fixes" into a future version, or just document on the wiki?

jdtsmith avatar Jun 30 '24 21:06 jdtsmith

That devdocs--rendering-functions is quite neat 👍 I was browsing the clojure docs and noticed that shr renders the var names and types as a single word, as they are just two consecutive spans in the DOM. Even with my very poor elisp skills I managed to easily adapt the above example to work around the issue.

Just in case any other clojure & devdocs.el user stumbles upon this:

(defun my--render-clojure-var-type (dom)
  "Fix how clojure var names and types were rendered in shr.
In other words, display \"assoc [function]\" instead of \"assocfunction\"."
  (if-let ((attrs (nth 1 dom))
           (dom-id (alist-get 'id attrs))
           ((string= "var-type" dom-id)))
      (progn
        (shr-insert " [")
        (shr-tag-span dom)
        (shr-insert "]"))
    (shr-tag-span dom)))
(push '(span . my--render-clojure-var-type) (alist-get 'clojure devdocs--rendering-functions))

lassemaatta avatar Jan 01 '25 07:01 lassemaatta