-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathorg-capture-ref.el
3369 lines (3097 loc) · 178 KB
/
org-capture-ref.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-capture-ref.el --- Extract bibtex info from captured websites -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Ihor Radchenko
;; Author: Ihor Radchenko <[email protected]>
;; Version: 0.3
;; Package-Requires: ((org "9.3") (persid) (compat) (doct "3.1.0"))
;; Keywords: tex, multimedia, bib
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This is a wrapper to `org-capture-templates' that automatically
;; extracts useful meta-information from the captured URLs. The
;; information is saved into bitex entry and can be reused to fill the
;; capture template.
;;; Code:
(require 'org-capture)
(require 'compat)
(require 'persid)
(require 'url-util)
(require 'bibtex)
(require 'dom)
;;; Customization:
(defgroup org-capture-ref nil
"Generation of bibtex info for captured webpages."
:tag "Org capture bibtex generator"
:group 'org-capture)
(defcustom org-capture-ref-get-buffer-functions '(org-capture-ref-get-buffer-from-html-file-in-query
org-capture-ref-retrieve-url)
"Functions used to retrieve html buffer for the captured link.
Each function will be called without arguments in sequence.
First non-nil return value of these functions will be used as buffer
containing html source of the link.
These functions will be called only when `org-capture-ref-get-buffer' is invoked from anywhere."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-get-bibtex-functions
'( org-capture-ref-set-default-type
org-capture-ref-set-access-date
org-capture-ref-set-access-date-timestamp
;; Handle capturing from Emacs buffers
org-capture-ref-get-bibtex-from-emacs-buffer
;; Pull generic data from capture
org-capture-ref-get-bibtex-url-from-capture-data
org-capture-ref-get-bibtex-howpublished-from-url
;; RSS parser
org-capture-ref-get-rss
;; Elfeed parsers
org-capture-ref-get-bibtex-from-elfeed-data
;; DOI retrieval
org-capture-ref-get-bibtex-doi
org-capture-ref-get-bibtex-isbn
org-capture-ref-get-bibtex-aps
org-capture-ref-get-bibtex-springer
org-capture-ref-get-bibtex-wiley
org-capture-ref-get-bibtex-tandfonline
org-capture-ref-get-bibtex-ieee
org-capture-ref-get-bibtex-semanticscholar
org-capture-ref-get-bibtex-sciencedirect-article
org-capture-ref-get-bibtex-sciencemag-careers-article
org-capture-ref-get-bibtex-nature-careers-article
org-capture-ref-get-bibtex-proquest
org-capture-ref-get-bibtex-arxiv
org-capture-ref-get-bibtex-ams-cn
org-capture-ref-get-bibtex-ohiolink
;; Site-specific parsing
org-capture-ref-get-bibtex-wordpress
org-capture-ref-get-bibtex-google-scholar-bibtex-page
org-capture-ref-get-bibtex-wikipedia-book
org-capture-ref-get-bibtex-wiki
org-capture-ref-get-bibtex-goodreads
org-capture-ref-get-bibtex-amazon
org-capture-ref-get-bibtex-github-commit
org-capture-ref-get-bibtex-github-issue
org-capture-ref-get-bibtex-github-pull-request
org-capture-ref-get-bibtex-github-repo
org-capture-ref-get-bibtex-github-file
org-capture-ref-get-bibtex-gitlab-commit
org-capture-ref-get-bibtex-gitlab-repo
org-capture-ref-get-bibtex-git-savannah-gnu-org-commit
org-capture-ref-get-bibtex-gnu-org
org-capture-ref-get-bibtex-gnu-org-manual
org-capture-ref-get-bibtex-nullprogram
org-capture-ref-get-bibtex-srht-repo
org-capture-ref-get-bibtex-reddit-wiki
org-capture-ref-get-bibtex-reddit-planetemacs
org-capture-ref-get-bibtex-reddit-comment
org-capture-ref-get-bibtex-reddit
org-capture-ref-get-bibtex-bilibili
org-capture-ref-get-bibtex-youtube-watch
org-capture-ref-get-bibtex-youtube-channel
org-capture-ref-get-bibtex-habr
org-capture-ref-get-bibtex-weixin
org-capture-ref-get-bibtex-samlib-book
org-capture-ref-get-bibtex-fanfics-me
org-capture-ref-get-bibtex-authortoday-reader
org-capture-ref-get-bibtex-authortoday-work
org-capture-ref-get-bibtex-authortoday-post
org-capture-ref-get-bibtex-fantlab-author
org-capture-ref-get-bibtex-fantlab-work
org-capture-ref-get-bibtex-fantlab-edition
org-capture-ref-get-bibtex-ficbook
org-capture-ref-get-bibtex-lesswrong
org-capture-ref-get-bibtex-archive-book
org-capture-ref-get-bibtex-stallman
org-capture-ref-get-bibtex-karl-voit
org-capture-ref-get-bibtex-imdb-movie
org-capture-ref-get-bibtex-orgmode-ml
org-capture-ref-get-bibtex-list-gnu-ml
org-capture-ref-get-bibtex-debbugs
org-capture-ref-get-bibtex-steam
org-capture-ref-get-bibtex-google-books
;; OpenGraph parser
org-capture-ref-parse-opengraph
;; Try to guess DOI and fetch the
;; DOI's BiBTeX.
org-capture-ref-get-bibtex-from-first-doi
;; Generic parser
org-capture-ref-parse-generic)
"Functions used to generate bibtex entry for captured link.
Each function will be called without arguments in sequence. The
functions are expected to use `org-capture-ref-set-bibtex-field' and
`org-capture-ref-set-capture-info'. to set the required bibtex fields.
`org-capture-ref-get-bibtex-field' and
`org-capture-ref-get-capture-info' can be used to retrieve information
about the captured link. Any function can throw an error and abort the
capture process. Any function can throw `:finish'. All the remaining
functions from this list will not be called then.
Any function can mark a field as not defined for the captured link.
This is done by setting that field to
`org-capture-ref-placeholder-value'. The following parsers will then
be aware that there is no need to search for the field."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-get-bibtex-from-elfeed-functions
'(org-capture-ref-get-bibtex-generic-elfeed
org-capture-ref-get-bibtex-nature-elfeed
org-capture-ref-get-bibtex-habr-elfeed
org-capture-ref-get-bibtex-rgoswami-elfeed-fix-author
org-capture-ref-get-bibtex-reddit-elfeed-fix-howpublished
org-capture-ref-get-bibtex-ted-elfeed)
"Functions used to generate BibTeX entry from elfeed entry data.
The data is defined in `:elfeed-data' field of the `org-protocol'
capture query.
This variable is only used if
`org-capture-ref-get-bibtex-from-elfeed-data' is listed in
`org-capture-ref-get-bibtex-functions'. The functions must follow the
same rules as `org-capture-ref-get-bibtex-functions', but will be
called with a single argument - efleed entry object.
These functions will only be called if `:elfeed-data' field is present
in `:query' field of the `org-store-link-plist'."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-get-bibtex-from-emacs-buffer-functions
'(org-capture-ref-get-url-at-point
org-capture-ref-get-bibtex-bibtex-mode
org-capture-ref-get-bibtex-eaf-browser
org-capture-ref-get-bibtex-eww
org-capture-ref-get-bibtex-notmuch-show-mode
org-capture-ref-get-bibtex-notmuch-search-mode
org-capture-ref-get-bibtex-notmuch-tree-mode
org-capture-ref-update-bibtex-at-org-heading)
"Functions used to generate BibTeX entry from Emacs buffer.
The buffer is at point or defined in `:buffer-marker' field of the
`org-protocol' capture query.
This variable is only used if
`org-capture-ref-get-bibtex-from-emacs-buffer' is listed in
`org-capture-ref-get-bibtex-functions'. The functions must follow the
same rules as `org-capture-ref-get-bibtex-functions' and will be
called with point at location to be captured.
These functions will only be called if `:buffer-marker' field is
present in `:query' field of the `org-store-link-plist'."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-clean-bibtex-hook
'(org-capture-ref-create-key-maybe
org-capture-ref-remove-double-comma
org-capture-ref-add-missing-comma
org-capture-ref-clean-doi
org-capture-ref-clean-pages
org-capture-ref-clean-bibtex-entry
org-capture-ref-fix-spacing
org-capture-ref-clear-nil-bibtex-entries
org-capture-ref-normalize-type
org-capture-ref-replace-@
org-capture-ref-remove-garbage-symbols-from-authors
org-capture-ref-capitalize-author)
"Normal hook containing functions used to cleanup BiBTeX entry string.
Each function is called with point at undefined position inside buffer
containing a single BiBTeX entry. The buffer is set to `bibtex-mode'.
The functions have access to `org-capture-ref-get-bibtex-field' and
`org-capture-ref-set-bibtex-field', but there is no guarantee that the
returned value is (or will be) in sync with the BiBTeX entry in the
buffer. It is recommended to use `bibtex-set-field' or
`bibtex-parse-entry' directly.
The new BiBTeX string will be parsed back into the BiBTeX data
structure, and thus may affect anything set by
`org-capture-ref-set-bibtex-field'."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-get-formatted-bibtex-functions '(org-capture-ref-get-formatted-bibtex-default)
"Functions used to format BiBTeX entry string.
Each function will be called without arguments in sequence.
`org-capture-ref-get-bibtex-field' and `org-capture-ref-get-capture-info' can
be used to retrieve information about the captured link.
Return value of the first function returning non-nil will be used as final format."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-generate-key-functions '(org-capture-ref-generate-key-human-readable)
"Functions used to generate citation key if it is not yet present.
The functions will be called in sequence until any of them returns non-nil value."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-check-bibtex-functions '(org-capture-ref-check-key
org-capture-ref-check-doi
org-capture-ref-check-url
org-capture-ref-check-link
org-capture-ref-check-article-title
org-capture-ref-check-author-title)
"Functions used to check the validity of generated BiBTeX.
The functions are called in sequence without arguments.
Any function can throw an error and abort the capture process.
Any function can throw `:finish'. All the remaining functions from
this list will not be called then."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-message-functions '(org-capture-ref-message-qutebrowser
;; This should be last
org-capture-ref-message-emacs)
"List of functions used to report the progress/errors during capture.
The functions must accept one or two arguments: message and severity.
Severity is one of symbols `info', `warning', `error'.
The last default function in this hook `org-capture-ref-message-emacs'
may throw error and hence prevent any laster function to be executed."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-headline-format-function #'org-capture-ref-headline-format
"Function with no arguments returning the headline text for `org-capture-ref-get-org-entry'."
:type 'function
:group 'org-capture-ref)
(defcustom org-capture-ref-headline-tags '("BOOKMARK" :type :typealt)
"List of tags to be added to org entry in `org-capture-ref-get-org-entry'.
Each element of the list can be either a string representing the tag
or a symbol representing the metadata to be used as a tag."
:type '(repeat (choice string symbol))
:group 'org-capture-ref)
(defcustom org-capture-ref-capture-target `(:file ,(file-name-concat org-directory "inbox.org"))
"Capture target for `org-capture-ref-capture-template'.
The specification will be inserted as is into a `doct' template. See
Target section of the `doct' docstring for details."
:type 'sexp
:group 'org-capture-ref)
(defcustom org-capture-ref-capture-keys '("z" "Z")
"List of the capture template keys to be used for interactive and silent templates respectively."
:type '(list string string)
:group 'org-capture-ref)
(defcustom org-capture-ref-capture-template
`( :group "org-capture-ref template"
:type entry
,@org-capture-ref-capture-target
:fetch-bibtex (lambda () (org-capture-ref-process-capture)) ; this must run first
:link-type (lambda () (org-capture-ref-get-bibtex-field :type))
:extra (lambda () (if (org-capture-ref-get-bibtex-field :journal)
(string-join
'("- [ ] [[elisp:(browse-url (url-encode-url (format \"https://sci-hub.se/%s\" (org-entry-get nil \"DOI\"))))][downlaod and attach pdf]]"
"- [ ] [[elisp:org-attach-open][read paper capturing interesting references]]"
"- [ ] [[elisp:(browse-url (url-encode-url (format \"https://www.semanticscholar.org/search?q=%s\" (org-entry-get nil \"TITLE\"))))][check citing articles]]"
"- [ ] [[elisp:(browse-url (url-encode-url (format \"https://www.connectedpapers.com/search?q=%s\" (org-entry-get nil \"TITLE\"))))][check related articles]]"
"- [ ] check if bibtex entry has missing fields"
"- [ ] Consider subscribing to new citations")
"\n")
""))
:org-entry (lambda () (org-capture-ref-get-org-entry))
:template
("%{fetch-bibtex}* TODO %?%{space}%{org-entry}"
"%{extra}")
:children (("Interactive org-capture-ref template"
:keys ,(car org-capture-ref-capture-keys)
:space " ")
("Silent org-capture-ref template"
:keys ,(cadr org-capture-ref-capture-keys)
:space ""
:immediate-finish t)))
"Default capture template.
The template is a template defined using `doct' syntax. See docstring
of `doct' for details."
:type 'list
:group 'org-capture-ref)
;;;; Customisation for default functions
(defcustom org-capture-ref-field-rules `((:doi . ("scheme=\"doi\" content=\"\\([^\"]*?\\)\""
"citation_doi\" content=\"\\([^\"]*?\\)\""
"data-doi=\"\\([^\"]*?\\)\""
"content=\"\\([^\"]*?\\)\" name=\"citation_doi"
"objectDOI\" : \"\\([^\"]*?\\)\""
"doi = '\\([^']*?\\)'"))
(:year . ((:meta "citation_publication_date" :apply ,#'org-capture-ref-extract-year-from-string)
"class=\\(?:date.[^>]*?\\)>[^<]*?\\([0-9]\\{4\\}\\)[^<]*?</"))
(:author . ((:meta "author")
(:meta "citation_author")
"\\(?:<[^>]*?class=\"author[^\"]*name\"[^>]*>\\([^<]+\\)<\\)"))
(:title . ((:meta "citation_title")
"<title.?+?>\\([[:ascii:][:nonascii:]]*?\\|.+\\)</title>")))
"Alist holding rules to populate common BiBTeX fields from html.
Used by `org-capture-ref-parse-generic'
Keys of the alist are the field names (example: `:author') and the
values are lists of regexps or `org-capture-ref-query-dom' rules. The
regexps are searched one by one in the html buffer and the group 1
match is used as value in the BiBTeX field."
:group 'org-capture-ref
:type '(alist :key-type symbol :value-type (set string (set symbol string))))
(defcustom org-capture-ref-demand-doi-list '("nature\\.com"
"aip\\.scitation\\.org"
"worldscientific\\.com"
"cambridge\\.org")
"List of regexps matching URLs that must have DOI.
If DOI retrieval fails on these URLs, fallback options are not used -
the capture exits with error."
:group 'org-capture-ref
:type '(list string))
(defcustom org-capture-ref-default-type "misc"
"Default BiBTeX type of the captured entry."
:group 'org-capture-ref
:type 'string)
(defcustom org-capture-ref-placeholder-value "unused"
"Key value indicating that this key is not applicable for the captured entry.
There is no need to attempt finding the value for this key.")
(defcustom org-capture-ref-default-bibtex-template "@${:type}{${:key},
typealt = {${:typealt}},
author = {${:author}},
title = {${:title}},
journal = {${:journal}},
school = {${:school}},
volume = {${:volume}},
number = {${:number}},
pages = {${:pages}},
year = {${:year}},
doi = {${:doi}},
isbn = {${:isbn}},
url = {${:url}},
howpublished = {${:howpublished}},
publisher = {${:publisher}},
keywords = {${:keywords}},
note = {Online; accessed ${:urldate}},
created = {${:created}},
effort = {${:effort}},
link = {${:link}},
rss = {${:rss}}
}"
"Default template used to format BiBTeX entry.
If a keyword from the template is missing, it will remain empty."
:type 'string
:group 'org-capture-ref)
(defcustom org-capture-ref-use-journal-abbreviations t
"Shorten journal/howpublished names in `org-capture-ref-headline-format'.
The full names are replaced in the hadline by
`org-capture-ref-journal-abbreviations' or
`org-ref-bibtex-journal-abbreviations'."
:type 'boolean
:group 'org-capture-ref)
(defcustom org-capture-ref-journal-abbreviations
'(("Nature Materials" . "NatureMat")
("Annual Review of Materials Research" . "AnnRevMatRes")
("Transactions of the Japan Institute of Metals" . "JIM")
("Physical Review Materials" . "PRMat")
("Physical Review Letters" . "PRL")
("Materials Science and Engineering: A" . "MSEA")
("Acta Materialia" . "ActaMat")
("Acta Metallurgica" . "ActaMat")
("Scripta Materialia" . "ScriptaMat")
("Journal of Materials Research" . "JMR")
("Advanced Engineering Materials" . "AdvEngMat")
("Philosophical Magazine" . "PhilMag")
("International Journal of Plasticity" . "IJP")
("physica status solidi (a)" . "PhysStatusSolidi(a)")
("Applied Physics Letters" . "APL")
("Philosophical Magazine Letters" . "PhilMagLett")
("Journal of Applied Physics" . "JAP")
("Accounts of Chemical Research" . "ACR")
("Acc. Chem. Res." . "ACR")
("ACS Catalysis" . "ACAT")
("ACS Catal." . "ACAT")
("Acta Metallurgica et Materialia" . "AMM")
("Acta Metall. Mater." . "AMM")
("Advanced Energy Materials" . "AEM")
("Adv. Energy Mater." . "AEM")
("ACS Applied Materials \\& Interfaces" . "AAMI")
("ACS Appl. Mater. Interfaces" . "AAMI")
("American Mineralogist" . "AMiner")
("Am. Mineral." . "AMiner")
("Angewandte Chemie-International Edition" . "AngC")
("Angew. Chem. Int. Edit." . "AngC")
("APL Materials" . "APLM")
("APL Mat." . "APLM")
("Applied Catalysis B: Environmental" . "ACBE")
("Appl. Catal. B-Environ." . "ACBE")
("Applied Physics Letters" . "APL")
("Appl. Phys. Lett." . "APL")
("Applied Surface Science" . "ASS")
("Appl. Surf. Sci." . "ASS")
("Catalysis Letters" . "CL")
("Catal. Lett." . "CL")
("Catalysis Communications" . "CC")
("Catal. Commun." . "CC")
("Catalysis Science & Technology" . "CST")
("Catal. Sci. Technol." . "CST")
("Catalysis Today" . "CT")
("Catal. Today" . "CT")
("Chemical Communications" . "ChC")
("Chem. Commun." . "ChC")
("Chemical Physics Letters" . "CPL")
("Chem. Phys. Lett" . "CPL")
("Chemical Reviews" . "CR")
("Chem. Rev." . "CR")
("Chemical Society Reviews" . "CSR")
("Chem. Soc. Rev." . "CSR")
("Chemical Society Reviews" . "CSR")
("Chem. Soc. Rev." . "CSR")
("Chemistry of Materials" . "CM")
("Chem. Mater." . "CM")
("Colloids and Surfaces, A: Physicochemical and Engineering Aspects" . "CSA")
("Colloids Surf., A" . "CSA")
("Combustion and Flame" . "CF")
("Combust. Flame" . "CF")
("Computational Materials Science" . "CPMS")
("Comp. Mater. Sci." . "CPMS")
("Computer Physics Communications" . "CPC")
("Comput. Phys. Commun." . "CPC")
("Computing in Science \\& Engineering" . "CSE")
("Comput. Sci. Eng." . "CSE")
("Crystal Growth \\& Design" . "CGD")
("Cryst. Growth Des." . "CGD")
("CrystEngComm" . "CEC")
("CrystEngComm" . "CEC")
("Electrochimica Acta" . "EA")
("Electrochim. Acta" . "EA")
("ECS Transactions" . "ECST")
("ECS Trans." . "ECST")
("Energy \\& Environmental Science" . "EES")
("Energy Environ. Sci." . "EES")
("High Pressure Research" . "HPR")
("High Pressure Res." . "HPR")
("Inorganic Chemistry" . "IC")
("Inorg. Chem." . "IC")
("Industrial \\& Engineering Chemistry Research" . "IECR")
("Ind. Eng. Chem. Res." . "IECR")
("Japanese Journal of Applied Physics" . "JJAP")
("Jpn. J. Appl. Phys." . "JJAP")
("Journal of Materials Research" . "JMatR")
("J. Mater. Res." . "JMatR")
("Journal of Alloys and Compounds" . "JALC")
("J. Alloy Compd." . "JALC")
("Journal of Applied Crystallography" . "JAC")
("J. Appl. Crystallogr." . "JAC")
("Journal of Applied Electrochemistry" . "JAE")
("J. Appl. Electrochem." . "JAE")
("Journal of Applied Physics" . "JAP")
("J. Appl. Phys." . "JAP")
("Journal of Catalysis" . "JC")
("J. Catal." . "JC")
("Journal of Chemical Physics" . "JCP")
("J. Chem. Phys." . "JCP")
("Journal of Computational Chemistry" . "JCC")
("J. Comput. Chem." . "JCC")
("Journal of Crystal Growth" . "JCG")
("J. Crys. Growth" . "JCG")
("Journal of Materials Chemistry" . "JMC")
("J. Mater. Chem." . "JMC")
("Journal of Materials Chemistry" . "JMC")
("J. Mater. Chem." . "JMC")
("Journal of Materials Science Letters" . "JMSL")
("J. Mater. Sci. Lett." . "JMSL")
("Journal of Membrane Science" . "JMS")
("J. Memb. Sci." . "JMS")
("Journal of Phase Equilibria" . "JPE")
("J. Phase Equilib." . "JPE")
("Journal of Physics and Chemistry of Solids" . "JPCS")
("J. Phys. Chem. Solids" . "JPCS")
("Journal of Physics: Condensed Matter" . "JPCM")
("J. Phys.: Condens. Matter" . "JPCM")
("Journal of Power Sources" . "JPS")
("J. Power Sources" . "Journal of Power Sources")
("Journal of Solid State Chemistry" . "JSSC")
("J. Solid State Chem." . "Journal of Solid State Chemistry")
("Journal of the American Ceramic Society" . "JACerS")
("J. Am. Ceram. Soc." . "JACerS")
("Journal of the American Chemical Society" . "JACS")
("J. Am. Chem. Soc." . "JACS")
("Journal of the American Society for Information Science and Technology" . "JASIST")
("J. Am. Soc. Inf. Sci. Technol." . "JASIST")
("Journal of The Electrochemical Society" . "JES")
("J. Electrochem. Soc." . "JES")
("Journal of Electroanalytical Chemistry" . "JEaC")
("J. Electroanal. Chem." . "JEaC")
("Journal of Membrane Science" . "JMS")
("J. Memb. Sci." . "JMS")
("Journal of Raman Spectroscopy" . "JRS")
("J. Raman Spectrosc." . "JRS")
("Journal of Vacuum Science \\& Technology A" . "JVST")
("J. Vac. Sci. Technol. A" . "JVST")
("Materials Letters" . "ML")
("Mater. Lett." . "ML")
("Materials Science and Engineering B" . "MSE-BS")
("Mat. Sci. Eng. B-Solid" . "MSE-BS")
("Molecular Simulation" . "MOLSIM")
("Mol. Sim." . "MOLSIM")
("Nature Materials" . "NM")
("Nat. Mater." . "NM")
("Nature Chemistry" . "NC")
("Nat. Chem." . "NC")
("Philosophical Magazine Letters" . "PML")
("Phil. Mag. Lett." . "PML")
("Philosophical Magazine A" . "PMA")
("Phil. Mag. A" . "PMA")
("Physica A: Statistical Mechanics and its Applications" . "PA")
("Physica A" . "PA")
("Physica B-Condensed Matter" . "PB")
("Physica B" . "PB")
("Physical Chemistry Chemical Physics" . "PCCP")
("Phys. Chem. Chem. Phys." . "PCCP")
("physica status solidi (b)" . "PSSB")
("Phys. Status Solidi B" . "PSSB")
("Physical Review A" . "PRA")
("Phys. Rev. A" . "PRA")
("Physical Review B" . "PRB")
("Phys. Rev. B" . "PRB")
("Physical Review Letters" . "PRL")
("Phys. Rev. Lett." . "PRL")
("Physics and Chemistry of Minerals" . "PCM")
("Phys. Chem. Miner." . "PCM")
("Proceedings of the National Academy of Sciences of the United States of America" . "PNAS")
("Proc. Natl. Acad. Sci. U. S. A." . "PNAS")
("Progress in Surface Science" . "PSurfSci")
("Prog. Surf. Sci." . "PSurfSci")
("Sensors and Actuators B: Chemical" . "SABC")
("Sensor. Actuat. B-Chem." . "SABC")
("Surface Science" . "SS")
("Surf. Sci." . "SS")
("The European Physical Journal B" . "EPJB")
("Eur. Phys. J. B" . "EPJB")
("The Journal of Physical Chemistry" . "JPC")
("J. Phys. Chem." . "JPC")
("The Journal of Physical Chemistry B" . "JPCB")
("J. Phys. Chem. B" . "JPCB")
("The Journal of Physical Chemistry C" . "JPCC")
("J. Phys. Chem. C" . "JPCC")
("The Journal of Physical Chemistry Letters" . "JPCL")
("J. Phys. Chem. Lett." . "JPCL")
("The Journal of Chemical Physics" . "JCP")
("J. Chem. Phys." . "JCP")
("Modelling and Simulation in Materials Science and Engineering" . "MSMSE")
("Modell. Simul. Mater. Sci. Eng." . "MSMSE")
("Thin Solid Films" . "TSF")
("Thin Solid Films" . "TSF")
("Topics in Catalysis" . "TC")
("Top. Catal." . "TC")
("Water Research" . "WR")
("Water Res." . "WR"))
"List of personal journal abbreviations. See `org-capture-ref-use-journal-abbreviations'."
:type '(list (cons string string))
:group 'org-capture-ref)
(defcustom org-capture-ref-parse-rss t
"When non-nil, try to find RSS feed of the page."
:type 'boolean
:group 'org-capture-ref)
(defcustom org-capture-ref-get-rss-functions '( org-capture-ref-get-rss-youtube-channel
org-capture-ref-get-rss-youtube-playlist
org-capture-ref-get-rss-reddit
org-capture-ref-get-rss-github-repo
org-capture-ref-get-rss-github-user
org-capture-ref-get-rss-generic)
"List of functions used to parse RSS."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-check-regexp-method 'grep
"Search method in `org-capture-ref-check-regexp'.
This variable affects `org-capture-ref-check-url' and `org-capture-ref-check-link'."
:type '(choice (const :tag "Use Unix grep" grep)
(const :tag "Use `org-search-view'" org-search-view))
:group 'org-capture-ref)
(defcustom org-capture-ref-check-key-method 'grep
"Search method in `org-capture-ref-check-key' when searching for IDs."
:type '(choice (const :tag "Use Unix grep" grep)
(const :tag "Use `org-id-find'" org-id-find))
:group 'org-capture-ref)
(defcustom org-capture-ref-check-link-regexp '((org-search-view . "^:\\(Source|URL\\+?\\):[ \t[]+%s[]]*$")
(grep . "^:(Source|URL\\+?):[ \t[]+%s[]]*$"))
"Regexp used to match the captured link against existing headlines.
`%s' is replaced by the url.
The value must be an alist of `org-capture-ref-check-regexp-method' and the corresponding regexp.")
(defcustom org-capture-ref-collection-types
'("playlist"
"author"
"proceedings"
"book"
"bookseries"
"collection")
"List of bibtex item types (or typealt) that can contain subordinate items."
:type '(list string)
:group 'org-capture-ref)
(defcustom org-capture-ref-fetch-collections t
"When non-nil, try capture items from currently captured URL.
Only takes effect when `org-capture-ref-capture-template-set-p' is set
and when :type or :typealt are listed in
`org-capture-ref-collection-types'."
:type 'boolean
:group 'org-capture-ref)
(defcustom org-capture-ref-fetch-collection-functions '( org-capture-ref-capture-collection-youtube)
"Functions used to capture contents of the current collection.
Each function will be called without arguments in sequence.
Each function is expected to call relevant capture functions like
`org-capture-ref-capture-url' with side-effects."
:type 'hook
:group 'org-capture-ref)
(defcustom org-capture-ref-warn-when-using-generic-parser t
"Non-nil means warn user when using generic metadata parser.
`debug' means show all the details."
:type 'boolean
:group 'org-capture-ref)
(defcustom org-capture-ref-quiet-verbosity t
"Show less messages.")
;;; API
(defmacro org-capture-ref-unless-set (fields &rest body)
"Run BODY unless all BiBTeX FIELDS are set."
(declare (debug (sexp body)) (indent 1))
`(unless (cl-every (lambda (key)
(org-capture-ref-get-bibtex-field key 'consider-placeholder))
,fields)
,@body))
(defun org-capture-ref-set-new-url (url)
"Reset environment as if capture was invoked for URL."
(org-capture-ref-reset-state)
(org-capture-ref-set-bibtex-field :url url)
;; Asquire the new URL.
(org-capture-ref-set-capture-info :link (org-capture-ref-get-bibtex-field :url))
(let ((org-capture-ref-get-buffer-functions '(org-capture-ref-retrieve-url)))
(org-capture-ref-get-buffer 'force)))
(defun org-capture-ref-get-buffer (&optional force)
"Return buffer containing contents of the captured link.
Retrieve the contents first if necessary or if FORCE is non-nil.
This calls `org-capture-ref-get-buffer-functions'."
(let ((buffer (or (and (not force) org-capture-ref--buffer)
(run-hook-with-args-until-success 'org-capture-ref-get-buffer-functions))))
(unless (buffer-live-p buffer) (org-capture-ref-message (format "<org-capture-ref> Failed to get live link buffer. Got %s" buffer) 'error))
(setq org-capture-ref--buffer-dom nil)
(with-current-buffer buffer
(let ((case-fold-search t))
(goto-char (point-min))
(when (re-search-forward "\\(Checking your browser before accessing\\|::CLOUDFLARE_ERROR_1000S_BOX::\\|https://report-uri\\.cloudflare\\.com/cdn-cgi/beacon/expect-ct\\)" nil t)
(org-capture-ref-message "URL is behind cloudflare firewall. Try to refresh/open the page in browser to whilelist current IP" 'error))))
(setq org-capture-ref--buffer buffer)))
(defun org-capture-ref-get-dom ()
"Return parsed html of the captured link."
(or org-capture-ref--buffer-dom
(setq org-capture-ref--buffer-dom (with-current-buffer (org-capture-ref-get-buffer)
(libxml-parse-html-region (point-min) (point-max))))))
(defun org-capture-ref-query-dom (&rest query)
"Query a dom element text from the website.
QUERY format:
:dom|:return-dom|:tag|:class|:id|:attr|:join|:meta|:apply value
[:tag|:class|:id|:attr|:join value|:apply]...
Value is a symbol, regexp, or regexp when matching for tag, class, or
id respectively. Value can be either a symbol or a cons (symbol .
string) for :attr. If value is a symbol, return the value of attribute
represented by that symbol. If value is the cons search dom elements
with attribute value equal to the strin in the cons.
:join sets a string to join multiple match. \" \" by default.
:dom sets dom to parse (default: org-capture-ref-get-dom).
:return-dom forces return value to be a DOM element instead of string
when non-nil.
:meta runs query to html metadata. All other query fields (except
:join) are ignored then. :meta must be the first symbol in the query.
:apply applies provided function symbol to the result of preceding
query."
(save-match-data
(let ((dom (if (eq ':dom (car query))
(prog1 (cadr query)
(setq query (cddr query)))
(org-capture-ref-get-dom)))
(return-dom (and (eq (car query) ':dom)
(prog1 (cadr query)
(setq query (cddr query)))))
(separator " "))
(while query
(unless (or (stringp dom)
(stringp (car dom))
(listp (car dom)))
(setq dom (list dom)))
(setq dom
(pcase (car query)
(:apply
(prog1 (funcall (cadr query) dom)
(setq query (cddr query))))
(:meta
(prog1 (org-capture-ref-query-meta (cadr query) (or (plist-get query :join) separator))
(setq query (cddr query))))
(:tag
(prog1 (mapcan (lambda (dom) (ensure-list (dom-by-tag dom (cadr query)))) dom)
(setq query (cddr query))))
(:class
(prog1 (mapcan (lambda (dom) (ensure-list (dom-by-class dom (cadr query)))) dom)
(setq query (cddr query))))
(:id
(prog1 (mapcan (lambda (dom) (ensure-list (dom-by-id dom (cadr query)))) dom)
(setq query (cddr query))))
(:attr
(pcase (cadr query)
((and (pred consp)
(app car name)
(app cdr value))
(prog1 (mapcan (lambda (dom) (ensure-list (dom-search dom (lambda (node) (string= value (dom-attr node name)))))) dom)
(setq query (cddr query))))
((pred symbolp)
(prog1 (mapcan (lambda (dom) (ensure-list (dom-attr dom (cadr query)))) dom)
(setq query (cddr query))))
(_ (error "Invalid :attr query: %s" (cadr query)))))
(:join
(prog1 dom
(setq separator (cadr query))
(setq query (cddr query))))
(_ (error "Invalid query: %s" query)))))
(if return-dom
dom
(decode-coding-string
(if (stringp dom)
dom
(unless (and (listp dom) (or (listp (car dom)) (stringp (car dom)))) (setq dom (list dom)))
(if (stringp (car dom))
(string-join (mapcar #'s-trim (delete "" dom)) separator)
(string-join (mapcar #'s-trim (delete "" (mapcar #'dom-texts dom))) separator)))
'utf-8)))))
(defun org-capture-ref-query-opengraph (key &optional separator)
"Query opengraph KEY from the website.
The KEY can be a symbol or string not prefixed with og:.
See https://ogp.me/ for possible KEY values.
SEPARATOR is separator used to concat array of KEYs (default is \" and \")."
(when (symbolp key) (setq key (symbol-name key)))
(setq key (concat "og:" key))
(let ((ans (string-join
(mapcar (lambda (node) (dom-attr node 'content))
(dom-search (org-capture-ref-get-dom)
(lambda (node)
(and (eq (car node) 'meta)
(or (string= key (dom-attr node 'property))
(string= key (dom-attr node 'name)))))))
(or separator " and "))))
(if (string-empty-p ans) nil ans)))
(defun org-capture-ref-query-meta (key &optional separator)
"Query KEY from the website metadata.
The KEY can be a symbol or string.
SEPARATOR is separator used to concat array of KEYs (default is \" and \")."
(when (symbolp key) (setq key (symbol-name key)))
(let ((ans (string-join
(mapcar (lambda (node) (replace-regexp-in-string " +" " " (dom-attr node 'content)))
(dom-search (org-capture-ref-get-dom)
(lambda (node)
(and (eq (car node) 'meta)
(or (string= key (dom-attr node 'property))
(string= key (dom-attr node 'itemprop)) ;; i.e. for Youtube video duration
(string= key (dom-attr node 'name)))))))
(or separator " and "))))
(if (string-empty-p ans) nil ans)))
(defun org-capture-ref-extract-year-from-string (string-or-dom)
"Extract year from STRING-OR-DOM date string or DOM element."
(let ((string (cond
((stringp string-or-dom) string-or-dom)
((stringp (car string-or-dom)) (mapconcat #'identity string-or-dom ""))
(t (dom-texts string-or-dom)))))
(when (and string (string-match "[0-9]\\{4\\}" string))
(match-string 0 string))))
(defun org-capture-ref-parse-timestamp (time)
"Parse ISO8601 TIME string.
See https://en.wikipedia.org/wiki/ISO_8601.
ISO8601 is, for example, used in Youtube video duration."
(when (string-match "^PT\\(?:\\([0-9]+\\)H\\)?\\(?:\\([0-9]+\\)M\\)?\\(?:\\([0-9]+\\)S\\)?" time)
(let ((hours (or (and (match-string 1 time)
(string-to-number (match-string 1 time)))
0))
(minutes (or (and (match-string 2 time)
(string-to-number (match-string 2 time)))
0))
(seconds (or (and (match-string 3 time)
(string-to-number (match-string 3 time)))
0)))
(when (>= minutes 60)
(cl-incf hours (floor (/ minutes 60)))
(setq minutes (% minutes 60)))
(when (> seconds 0) (cl-incf minutes))
;; We are interested in hours and
;; minuts. Drop seconds.
(format "%.2d:%.2d" hours minutes))))
(defun org-capture-ref-get-bibtex-field (field &optional return-placeholder-p)
"Return the value of the BiBTeX FIELD or nil the FIELD is not set.
Unless RETURN-PLACEHOLDER-P is non-nil, return nil when the value is equal
to `org-capture-ref-placeholder-value'.
FIELD must be a symbol like `:author'.
See `org-capture-ref--bibtex-alist' for common field names."
(if return-placeholder-p
(alist-get field org-capture-ref--bibtex-alist)
(let ((res (alist-get field org-capture-ref--bibtex-alist)))
(unless (and (stringp res) (string-equal res org-capture-ref-placeholder-value)) res))))
(defun org-capture-ref-get-capture-template-info (key)
"Return value of KEY from `org-capture-plist'."
(plist-get org-capture-plist key))
(defun org-capture-ref-get-capture-info (key)
"Return value of KEY from `org-capture-ref--store-link-plist'.
See docstring of `org-capture-ref--store-link-plist' for possible KEYs.
KEY can be a list, which means that the `car' of KEY is a plist
containing `cdar' of KEY, an so on."
(when (symbolp key) (setq key (list key)))
(let ((plist org-capture-ref--store-link-plist))
(while key
(setq plist (plist-get plist (pop key))))
plist))
(defun org-capture-ref-set-bibtex-field (field val &optional force)
"Set BiBTeX FIELD to VAL.
FIELD must be a symbol like `:author'.
See `org-capture-ref--bibtex-alist' for common field names.
If VAL is empty string, do not do anything.
Bypass VAL check when FORCE is non-nil."
(when (stringp val) (setq val (string-trim val)))
(unless (and (not force) (or (and (stringp val) (string-empty-p val)) (not val)))
(setf (alist-get field org-capture-ref--bibtex-alist) val)))
(defun org-capture-ref-set-capture-info (key val)
"Set KEY in capture info to VAL.
The KEY set here will be passed down to `org-capture' via
`org-store-link-plist'. See docstring of
`org-capture-ref--store-link-plist' for possible KEYs."
(plist-put org-capture-ref--store-link-plist key val))
;;; Predefined functions
;;;; Getting html buffer
(defun org-capture-ref-get-buffer-from-html-file-in-query ()
"Use buffer from file defined in `:html' field of `org-protocol' query."
(let* ((html (org-capture-ref-get-capture-info '(:query :html))))
(when html
(let ((coding-system-for-read 'utf-8))
(let ((auto-mode-alist '((".+" . fundamental-mode))))
(find-file-noselect html))))))
(defun org-capture-ref-retrieve-url ()
"Retrieve html buffer from `:link' field of capture data."
(let ((link (or (org-capture-ref-get-capture-info :link)
(org-capture-ref-get-bibtex-field :url))))
(when link
(url-retrieve-synchronously link))))
;;;; Getting RSS
(defun org-capture-ref-get-rss ()
"Parse RSS."
(when org-capture-ref-parse-rss
(catch :finish (run-hooks 'org-capture-ref-get-rss-functions))))
(defun org-capture-ref-get-rss-youtube-channel ()
"Parse RSS from YouTube channel page."
(when-let ((link (org-capture-ref-get-bibtex-field :url)))
(when (string-match "youtube\\.com/\\(channel\\|c\\)/" link)
(let* ((channel-url (org-capture-ref-query-dom :meta 'og:url))
(channel-id (and (string-match
"youtube\\.com/channel/\\([^/]+\\)"
channel-url)
(match-string 1 channel-url))))
(when channel-id
(org-capture-ref-set-bibtex-field
:rss (format "https://www.youtube.com/feeds/videos.xml?channel_id=%s" channel-id))
(throw :finish t))))))
(defun org-capture-ref-get-rss-youtube-playlist ()
"Parse RSS from YouTube playlist page."
(when-let ((link (org-capture-ref-get-bibtex-field :url)))
(when (string-match "youtube\\.com/playlist\\?list=\\([^/]+\\)" link)
(let* ((list-id (match-string 1 link)))
(when list-id
(org-capture-ref-set-bibtex-field
:rss (format "https://www.youtube.com/feeds/videos.xml?playlist_id=%s" list-id))
(throw :finish t))))))
(defun org-capture-ref-get-rss-reddit ()
"Parse RSS from reddit page."
(when-let ((link (org-capture-ref-get-bibtex-field :url)))
(when (string-match "reddit\\.com/" link)
(org-capture-ref-set-bibtex-field
:rss (format "%s.rss" link))
(throw :finish t))))
(defun org-capture-ref-get-rss-github-repo ()
"Parse RSS from GitHub repo page."
(when-let ((link (org-capture-ref-get-bibtex-field :url)))
(when (string-match "github\\.com/[^/]+/[^/]+$" link)
(org-capture-ref-set-bibtex-field
:rss (format "%s/commits.atom" link))
(throw :finish t))))
(defun org-capture-ref-get-rss-github-user ()
"Parse RSS from GitHub user page."
(when-let ((link (org-capture-ref-get-bibtex-field :url)))
(when (string-match "github\\.com/[^/]+$" link)
(org-capture-ref-set-bibtex-field
:rss (format "%s.atom" link))
(throw :finish t))))
(defun org-capture-ref-get-rss-generic ()
"Parse RSS.
This function was inspired by https://github.com/HKey/feed-discovery/.
This function follows specification as in
https://www.rssboard.org/rss-autodiscovery, but BASE is ignored."
(let ((rss-url (org-capture-ref-query-dom
:tag 'link