Skip to content

Commit cc4f32a

Browse files
committed
Cleanup lang-buffer and extract author from gb-book in text-top-x
1 parent 15d7176 commit cc4f32a

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

speed-type.el

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -989,11 +989,13 @@ Only the words which are in the boundaries START and END are considered."
989989

990990
(defun speed-type--retrieve-random-book-num (lang)
991991
"Get a random book-num which has the given LANG."
992-
(let ((lang-book-list (with-current-buffer (speed-type--retrieve (concat lang ".html")
993-
(concat "https://www.gutenberg.org/ebooks/search/?query=l." lang))
994-
(mapcar (lambda (h) (substring (dom-attr h 'href) 8))
995-
(mapcar (lambda (c) (dom-by-tag c 'a))
996-
(dom-by-class (libxml-parse-html-region (point-min) (point-max) nil) "booklink"))))))
992+
(let* ((lang-buffer (speed-type--retrieve (concat lang ".html")
993+
(concat "https://www.gutenberg.org/ebooks/search/?query=l." lang)))
994+
(lang-book-list (with-current-buffer lang-buffer
995+
(mapcar (lambda (h) (substring (dom-attr h 'href) 8))
996+
(mapcar (lambda (c) (dom-by-tag c 'a))
997+
(dom-by-class (libxml-parse-html-region (point-min) (point-max) nil) "booklink"))))))
998+
(kill-buffer lang-buffer)
997999
(string-to-number (nth (random (length lang-book-list)) lang-book-list))))
9981000

9991001
(defun speed-type--wordlist-retrieve (lang)
@@ -1751,12 +1753,17 @@ If `speed-type-default-lang' is set, will pick a random book of that language."
17511753
(let* ((book-num (if speed-type-default-lang
17521754
(speed-type--retrieve-random-book-num speed-type-default-lang)
17531755
(nth (random (length speed-type-gb-book-list)) speed-type-gb-book-list)))
1756+
(gb-buffer (speed-type--gb-retrieve book-num))
17541757
(buffer (speed-type--gb-top-retrieve book-num))
17551758
(fn (buffer-file-name buffer))
17561759
(char-length (+ speed-type-min-chars (random (- speed-type-max-chars speed-type-min-chars))))
17571760
(n (min x (with-current-buffer buffer (count-words (point-min) (point-max)))))
17581761
(buf (speed-type-prepare-content-buffer-from-buffer buffer))
17591762
(title (format "Top %s words of book %s" n book-num))
1763+
(author (with-current-buffer gb-buffer
1764+
(save-excursion
1765+
(when (re-search-forward "^Author: " nil t)
1766+
(buffer-substring (point) (line-end-position))))))
17601767
(text (with-temp-buffer
17611768
(while (< (buffer-size) char-length)
17621769
(let ((random-word (speed-type--get-random-word buf n)))
@@ -1767,12 +1774,13 @@ If `speed-type-default-lang' is set, will pick a random book of that language."
17671774
(buffer-string))))
17681775
(add-extra-word-content-fn (lambda () (speed-type--get-random-word buf n)))
17691776
(go-next-fn (lambda () (speed-type--get-next-top-fn x))))
1777+
(kill-buffer gb-buffer) ;; buffer is retrieved, remove it again to not clutter the buffer-list
17701778
(kill-buffer buffer) ;; buffer is retrieved, remove it again to not clutter the buffer-list
17711779
(speed-type--setup buf
17721780
text
17731781
:file-name fn
17741782
:title title
1775-
:author "To be extracted from gb-book"
1783+
:author author
17761784
:n-words n
17771785
:replay-fn #'speed-type--get-replay-fn
17781786
:go-next-fn go-next-fn

0 commit comments

Comments
 (0)