• 2 Posts
  • 25 Comments
Joined 1 year ago
cake
Cake day: October 3rd, 2023

help-circle







  • github-alphapapa@alien.topBtoEmacs@communick.newsRate my elisp
    link
    fedilink
    English
    arrow-up
    1
    ·
    11 months ago

    My first suggestion would be to use plz for HTTP. Then I’d use cl-loop and pcase to simplify the rest of the code. Here’s a partial rewrite with a TODO for further exercise. :)

    (defun wikipedia-article-references (subject)
      (let* ((url (format "https://en.wikipedia.org/wiki/%s" (url-hexify-string subject)))
             (dom (plz 'get url :as #'libxml-parse-html-region)))
        (cl-loop for cite-tag in (dom-by-tag dom 'cite)
                 for cite-class = (dom-attr cite-tag 'class)
                 collect (pcase cite-class
                           ((rx "journal")
                            (let ((a-tag (dom-search cite-tag
                                                     (lambda (tag)
                                                       (string-prefix-p "https://doi.org" (dom-attr tag 'href))))))
                              (cons (concat "doi:" (dom-text a-tag))
                                    ;; TODO: Use `string-match' with `rx' and `match-string' here.
                                    (let* ((cite-texts (dom-texts cite-tag))
                                           (title-beg (1+ (string-search "\"" cite-texts)))
                                           (title-end (string-search "\"" cite-texts (1+ title-beg))))
                                      (substring cite-texts title-beg title-end)))))
                           ((rx "book")
                            (let ((a-tag (dom-search cite-tag
                                                     (lambda (tag)
                                                       (string-prefix-p "/wiki/Special:BookSources" (dom-attr tag 'href))))))
                              (cons (concat "isbn:" (dom-text (dom-child-by-tag a-tag 'bdi)))
                                    (dom-text (dom-child-by-tag cite-tag 'i)))))
                           (_ (let ((a-tag (assoc 'a cite-tag)))
                                (cons (dom-attr a-tag 'href) (dom-text a-tag))))))))
    

    Regarding this:

    And yes, I know that I could probably use a library like s, dash, seq, or cl, but I try to keep my elisp functions free of those kind of things

    First of all, cl and seq are built-in to Emacs and are used in core Emacs code. There’s no reason not to use them. Second, dash and s are on ELPA and are widely used; it’s largely a matter of style, but they are solid libraries, so again, no reason not to use them. They don’t have cooties. ;)














  • Again, look at its history. Every comment sounds like it’s written by GPT. And it keeps talking about email marketing out of context. And it’s not participating in any communities, it’s just spamming comments randomly to farm karma. And, look, 3.2k comment karma on an 8-day-old account. Does that really seem legit to you?