-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
shrface.el
1887 lines (1701 loc) · 75.3 KB
/
shrface.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
;;; shrface.el --- Extend shr/eww with org features and analysis capability -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Damon Chan
;; Author: Damon Chan <[email protected]>
;; URL: https://github.com/chenyanming/shrface
;; Keywords: faces
;; Created: 10 April 2020
;; Version: 2.6.4
;; Package-Requires: ((emacs "25.1") (org "9.0") (language-detection "0.1.0"))
;; 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 <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package extends `shr' / `eww' with org features and analysis capability.
;; It can be used in `dash-docs', `eww', `nov.el', `mu4e', `anki.el', etc.
;; - Configurable org-like heading faces, headline bullets, item bullets, paragraph
;; indentation, fill-column, item bullet, versatile hyper
;; links(http/https/file/mailto/etc) face and so on.
;; - Browse the internet or local html file with `eww' just like org mode.
;; - Read dash docsets with `dash-docs' and the beauty of org faces.
;; - Read epub files with `nov.el' , just like org mode.
;; - Read html email with `mu4e' , the same reading experience just like org mode
;; without formatting html to org file.
;; - Switch/jump the headlines just like org-mode in `eww' and `nov.el' with `imenu'
;; - Toggle/cycle the headlines just like org-mode in `eww' and `nov.el' with `outline-minor-mode'
;; and `org-cycle'/`org-shifttab'
;; - Analysis capability:
;; - Headline analysis: List all headlines with clickable texts.
;; - URL analysis: List all classified URL with clickable texts.
;; - Export HTML buffer to an org file using shr engine (no Pandoc is needed).
;;; Code:
(require 'shr)
(require 'org)
(require 'org-table)
(require 'org-faces)
(require 'outline)
(require 'compile)
(require 'pcase)
(require 'cl-lib)
(require 'cl-macs)
(require 'url)
(ignore-errors
;; in case the users lazy load org-mode before require shrface
;; require org-superstar and org-bullets
(require 'org-superstar)
(require 'org-bullets)
(require 'all-the-icons)
(require 'consult)
(require 'ivy)
(require 'helm)
(require 'helm-utils))
;;; shrface
(defgroup shrface nil
"Org-like faces setting for shr"
:group 'shr)
(defgroup shrface-faces nil
"Org-like faces for shr"
:group 'shrface
:group 'faces)
(defgroup shrface-analysis-faces nil
"Faces for shrface analysis realted buffers"
:group 'shrface
:group 'faces)
(defcustom shrface-bullets-bullet-list
(or (bound-and-true-p org-bullets-bullet-list)
(bound-and-true-p org-superstar-headline-bullets-list)
'("◉"
"○"
"✸"
"✿"))
"Bullets for headings."
:group 'shrface
:type '(repeat (string :tag "Bullet character")))
(define-obsolete-variable-alias 'shrface-paragraph-indentation
'shr-indentation "shrface 2.6.4"
"Please use `shr-indentation'to control the indentation.")
(define-obsolete-variable-alias 'shrface-paragraph-fill-column
'shr-width "shrface 2.6.4"
"Please use `nov-text-width' for `nov-mode'. For other modes,
please use `shr-width.'")
(defcustom shrface-item-bullet '?–'
"Bullet used for unordered lists."
:group 'shrface
:type 'character)
(defcustom shrface-mode-hook nil
"Hook run after enable variable `shrface-mode' buffers.
This hook is evaluated when enable variable `shrface-mode'."
:group 'shrface
:type 'hook)
(defcustom shrface-href-versatile nil
"NON-nil to enable versatile href faces."
:group 'shrface
:type 'boolean)
(defcustom shrface-imenu-depth 5
"The maximum level for Imenu access to shrface headlines."
:group 'shrface
:type 'integer)
(defcustom shrface-toggle-bullets nil
"Non-nil to disable headline bullets globally.
The following features are also disabled:
1. function `shrface-occur'
2. variable `shrface-mode'"
:group 'shrface
:type 'boolean)
(defcustom shrface-ellipsis "…"
"The ellipsis to use in the shrface headlines."
:group 'shrface
:type 'string)
(defvar shrface-org nil
"NON-nil to render with original org features.")
(defvar shrface-org-title nil
"Title of exported org file.
Bind it to overwrite `shrface-title'.")
(defvar shrface-title nil
"Title of html.")
(defvar shrface-request-url nil
"Request url of html.
Bound it before generating the org buffer/file.")
(defvar shrface-headline-property 'shrface-headline
"Property name to use for href.")
(defvar shrface-headline-number-property 'shrface-number
"Property name to use for headline number.")
(defvar shrface-headline-number 0
"Counter to count The nth Headline in the buffer.")
(defvar shrface-href-property 'shr-url
"Property name to use for href.")
(defvar shrface-eww-image-property 'image-url
"Property name to use for internet image.")
(defvar shrface-href-follow-link-property 'follow-link
"Property name to use for follow link.")
;; (defvar shrface-nov-image-original-property 'display
;; "Property name to use for external image.")
(defvar shrface-href-face 'shrface-href-face
"Face name to use for href if `shrface-href-versatile' is nil.")
(defvar shrface-href-http-face 'shrface-href-http-face
"Face name to use for href http://.")
(defvar shrface-href-https-face 'shrface-href-https-face
"Face name to use for href https://.")
(defvar shrface-href-ftp-face 'shrface-href-ftp-face
"Face name to use for href ftp://.")
(defvar shrface-href-file-face 'shrface-href-file-face
"Face name to use for href file://.")
(defvar shrface-href-mailto-face 'shrface-href-mailto-face
"Face name to use for href mailto://.")
(defvar shrface-href-other-face 'shrface-href-other-face
"Face name to use for other href.")
(defvar shrface-level 'shrface-level
"Compute the header's nesting level in an outline.")
(defvar shrface-supported-faces-alist
'((em . shrface-tag-em)
(u . shrface-tag-u)
(strong . shrface-tag-strong)
(svg . shrface-tag-svg)
(h1 . shrface-tag-h1)
(h2 . shrface-tag-h2)
(h3 . shrface-tag-h3)
(h4 . shrface-tag-h4)
(h5 . shrface-tag-h5)
(h6 . shrface-tag-h6)
(a . shrface-tag-a)
(p . shrface-tag-p)
(li . shrface-tag-li)
(dt . shrface-tag-dt)
(figure . shrface-tag-figure)
;; (code . shrface-tag-code)
)
"Alist of shrface supported faces except experimental faces.")
(defvar shrface-href-collected-list nil
"Global list to save the collected href items.
Used when we run `shrface-links' or `shrface-links-counsel'.
Used for later analysis, sorting, exporting etc.")
(defvar shrface-headline-collected-list nil
"Global list to save the collected href items.
Used when we run `shrface-headline-counsel'.
Used for later analysis, sorting, exporting etc.")
(defface shrface-href-face '((t :inherit org-link))
"Default <href> face if `shrface-href-versatile' is nil"
:group 'shrface-faces)
(defface shrface-href-other-face '((t :inherit org-link :foreground "#81A1C1"))
"Face used for <href> other than http:// https:// ftp://
file:// mailto:// if `shrface-href-versatile' is NON-nil. For
example, it can be used for fontifying charter links with epub
files when using nov.el."
:group 'shrface-faces)
(defface shrface-href-http-face '((t :inherit org-link :foreground "#39CCCC"))
"Face used for <href>, http:// if `shrface-href-versatile' is
NON-nil"
:group 'shrface-faces)
(defface shrface-href-https-face '((t :inherit org-link :foreground "#7FDBFF"))
"Face used for <href>, https:// if `shrface-href-versatile' is
NON-nil"
:group 'shrface-faces)
(defface shrface-href-ftp-face '((t :inherit org-link :foreground "#5E81AC"))
"Face used for <href>, ftp:// if `shrface-href-versatile' is
NON-nil"
:group 'shrface-faces)
(defface shrface-href-file-face '((t :inherit org-link :foreground "#87cefa"))
"Face used for <href>, file:// if `shrface-href-versatile' is
NON-nil"
:group 'shrface-faces)
(defface shrface-href-mailto-face '((t :inherit org-link :foreground "#8FBCBB"))
"Face used for <href>, mailto:// if `shrface-href-versatile' is
NON-nil"
:group 'shrface-faces)
(defface shrface-h1-face '((t :inherit org-level-1))
"Face used for <h1> headlines."
:group 'shrface-faces)
(defface shrface-h2-face '((t :inherit org-level-2))
"Face used for <h2> headlines."
:group 'shrface-faces)
(defface shrface-h3-face '((t :inherit org-level-3))
"Face used for <h3> headlines."
:group 'shrface-faces)
(defface shrface-h4-face '((t :inherit org-level-4))
"Face used for <h4> headlines."
:group 'shrface-faces)
(defface shrface-h5-face '((t :inherit org-level-5))
"Face used for <h5> headlines."
:group 'shrface-faces)
(defface shrface-h6-face '((t :inherit org-level-6))
"Face used for <h6> headlines."
:group 'shrface-faces)
(defface shrface-highlight '((t :inherit mode-line-highlight))
";;Face used for highlight."
:group 'shrface-faces)
(defface shrface-verbatim '((t :inherit org-verbatim))
"Face used for verbatim/emphasis - <em>."
:group 'shrface-faces)
(defface shrface-code '((t :inherit org-code))
"TODO Face used for inline <code>"
:group 'shrface-faces)
(defface shrface-figure '((t :inherit org-table))
"Face used for figure <figure>, e.g. figure captions."
:group 'shrface-faces)
(defface shrface-item-bullet-face '((t :inherit org-list-dt))
"Face used for unordered list bullet"
:group 'shrface-faces)
(defface shrface-item-number-face '((t :inherit org-list-dt))
"Face used for ordered list numbers"
:group 'shrface-faces)
(defface shrface-description-list-term-face '((t :inherit org-list-dt))
"Face used for description list terms <dt>"
:group 'shrface-faces)
;;; Faces for shrface-analysis realted buffers
(defface shrface-links-title-face '((t :inherit default))
"Face used for *shrface-links* title"
:group 'shrface-analysis-faces)
(defface shrface-links-url-face '((t :inherit font-lock-comment-face))
"Face used for *shrface-links* url"
:group 'shrface-analysis-faces)
(defface shrface-links-mouse-face '((t :inherit mode-line-highlight))
"Face used for *shrface-links* mouse face"
:group 'shrface-analysis-faces)
(defvar-local shrface-outline--cycle-buffer-state 'show-all
"Interval variable used for tracking buffer cycle state.")
;;; Keys
(defvar shrface-mode-map
(let ((map (make-sparse-keymap)))
map)
"Keymap for function `shrface-mode'.")
(defun shrface-default-keybindings ()
"Setup default keybingings for variable `shrface-mode'."
(interactive)
"Sets up the default keybindings for `shrface-mode'."
(define-key shrface-mode-map (kbd "TAB") 'shrface-outline-cycle)
(define-key shrface-mode-map (kbd "<backtab>") 'shrface-outline-cycle-buffer)
(define-key shrface-mode-map (kbd "C-t") 'shrface-toggle-bullets)
(define-key shrface-mode-map (kbd "C-j") 'shrface-next-headline)
(define-key shrface-mode-map (kbd "C-k") 'shrface-previous-headline)
(define-key shrface-mode-map (kbd "M-l") 'shrface-links-counsel) ; or 'shrface-links-helm
(define-key shrface-mode-map (kbd "M-h") 'shrface-headline-counsel))
;;; Utility
;;;###autoload
(defun shrface-shr-fontize-dom (dom props face)
"Fontize the sub Optional argument DOM PROPS and FACE."
(let (start end) ;; remember start of inserted region
(setq start (point))
(shr-generic dom) ;; inserts the contents of the tag
(setq end (point))
(shrface-shr-add-props start end props)
(shrface-shr-add-face start end face))) ;; puts text properties of TYPES on the inserted contents
;;;###autoload
(defun shrface-shr-add-props (start end props)
"Fontize the string.
Argument START start point.
Argument END end point.
Argument PROPS text properties."
(save-excursion
(goto-char start)
(while (< (point) end)
(when (bolp)
(skip-chars-forward " "))
(add-text-properties (point) (min (line-end-position) end) props)
(if (< (line-end-position) end)
(forward-line 1)
(goto-char end)))))
;;;###autoload
(defun shrface-shr-add-face (start end face)
"Fontize the string.
Argument START start point.
Argument END end point.
Argument FACE face."
(save-excursion
(goto-char start)
(while (< (point) end)
(when (bolp)
(skip-chars-forward " "))
(add-face-text-property (point) (min (line-end-position) end) face)
(if (< (line-end-position) end)
(forward-line 1)
(goto-char end)))))
;;;###autoload
(defun shrface-shr-urlify (start url &optional title)
"Fontize the URL.
Argument START start point.
Argument END the url."
(shr-add-font start (point) 'shr-link)
(and shrface-href-versatile (stringp url)
(let* ((extract (let ((sub url)
(regexp "\\(https:\\)\\|\\(http:\\)\\|\\(ftp:\\)\\|\\(file:\\)\\|\\(mailto:\\)"))
(when (string-match regexp sub)
(match-string 0 sub))))
(match (cond
((equal extract "http:") shrface-href-http-face)
((equal extract "https:") shrface-href-https-face)
((equal extract "ftp:") shrface-href-ftp-face)
((equal extract "file:") shrface-href-file-face)
((equal extract "mailto:") shrface-href-mailto-face)
(t shrface-href-other-face))))
(add-text-properties
start (point)
(list 'shr-url url
'help-echo (let ((iri (or (ignore-errors
(decode-coding-string
(url-unhex-string url)
'utf-8 t))
url)))
(if title (format "%s (%s)" iri title) iri))
'follow-link t
'face match
`,match t
'mouse-face 'highlight)))
(add-text-properties
start (point)
(list 'shr-url url
'help-echo (let ((iri (or (ignore-errors
(decode-coding-string
(url-unhex-string url)
'utf-8 t))
url)))
(if title (format "%s (%s)" iri title) iri))
'follow-link t
'face shrface-href-face
'mouse-face 'highlight)))
(while (and start
(< start (point)))
(let ((next (next-single-property-change start 'keymap nil (point))))
(if (get-text-property start 'keymap)
(setq start next)
(put-text-property start (or next (point)) 'keymap shr-map)))))
;;;###autoload
(defun shrface-bullets-level-string (level)
"Return the bullets in cycle way.
Argument LEVEL the headline level."
(nth (mod (1- level)
(length shrface-bullets-bullet-list))
(if (characterp (car shrface-bullets-bullet-list))
(mapcar 'char-to-string shrface-bullets-bullet-list)
shrface-bullets-bullet-list)))
(defun shrface-outline-regexp ()
"TODO: Regexp to match shrface headlines."
(concat " ?+"
(regexp-opt
(if (characterp (car shrface-bullets-bullet-list))
(mapcar 'char-to-string shrface-bullets-bullet-list)
shrface-bullets-bullet-list)
t) " +"))
(defun shrface-outline-regexp-bol ()
"TODO: Regexp to match shrface headlines.
This is similar to `shrface-outline-regexp' but additionally makes
sure that we are at the beginning of the line."
(concat " ?+"
(regexp-opt
(if (characterp (car shrface-bullets-bullet-list))
(mapcar 'char-to-string shrface-bullets-bullet-list)
shrface-bullets-bullet-list)
t) "\\( +\\)"))
(defun shrface-imenu-regexp-bol ()
"TODO: Regexp to match shrface headlines.
This is similar to `shrface-outline-regexp' but additionally makes
sure that we are at the beginning of the line."
(concat "^\\(?: ?+\\)"
(regexp-opt
(if (characterp (car shrface-bullets-bullet-list))
(mapcar 'char-to-string shrface-bullets-bullet-list)
shrface-bullets-bullet-list)
t) "\\( .*\\)$"))
(defun shrface-clear (DOM)
"Clear the `shrface-headline-number'.
Argument DOM The DOM."
(listp DOM) ; just make the compile do not complain
(setq shrface-headline-number 0))
(defun shrface-tag-h1 (dom)
"Fontize tag h1.
Argument DOM dom."
(setq-local shrface-headline-number (1+ shrface-headline-number))
(shrface-shr-h1 dom `(shrface-headline "shrface-h1" ,shrface-headline-number-property ,shrface-headline-number) 'shrface-h1-face))
(defun shrface-tag-h2 (dom)
"Fontize tag h2.
Argument DOM dom."
(setq-local shrface-headline-number (1+ shrface-headline-number))
(shrface-shr-h2 dom `(shrface-headline "shrface-h2" ,shrface-headline-number-property ,shrface-headline-number) 'shrface-h2-face))
(defun shrface-tag-h3 (dom)
"Fontize tag h3.
Argument DOM dom."
(setq-local shrface-headline-number (1+ shrface-headline-number))
(shrface-shr-h3 dom `(shrface-headline "shrface-h3" ,shrface-headline-number-property ,shrface-headline-number) 'shrface-h3-face))
(defun shrface-tag-h4 (dom)
"Fontize tag h4.
Argument DOM dom."
(setq-local shrface-headline-number (1+ shrface-headline-number))
(shrface-shr-h4 dom `(shrface-headline "shrface-h4" ,shrface-headline-number-property ,shrface-headline-number) 'shrface-h4-face))
(defun shrface-tag-h5 (dom)
"Fontize tag h5.
Argument DOM dom."
(setq-local shrface-headline-number (1+ shrface-headline-number))
(shrface-shr-h5 dom `(shrface-headline "shrface-h5" ,shrface-headline-number-property ,shrface-headline-number) 'shrface-h5-face))
(defun shrface-tag-h6 (dom)
"Fontize tag h6.
Argument DOM dom."
(setq-local shrface-headline-number (1+ shrface-headline-number))
(shrface-shr-h6 dom `(shrface-headline "shrface-h6" ,shrface-headline-number-property ,shrface-headline-number) 'shrface-h6-face))
(defun shrface-shr-item-bullet ()
"Build a `shr-bullet' based on `shrface-item-bullet'."
(setq shr-bullet (concat (char-to-string shrface-item-bullet) " ")))
(defun shrface-shr-h1 (dom props face)
"Insert the Fontized tag h1.
Argument DOM dom.
Argument PROPS text properties.
Argument FACE face."
(shr-ensure-paragraph)
(if shrface-org
(progn
(insert "* ")
(let ((url (dom-attr (dom-by-tag dom 'a) 'href)))
(shrface-insert-org-link url dom)))
(unless shrface-toggle-bullets
(insert (propertize (concat (shrface-bullets-level-string 1) " ") 'face 'shrface-h1-face 'shrface-bullet "shrface-h1-bullet")))
(shrface-shr-fontize-dom dom props face))
(shr-ensure-paragraph))
(defun shrface-shr-h2 (dom props face)
"Insert the Fontized tag h2.
Argument DOM dom.
Argument PROPS text properties.
Argument FACE face."
(shr-ensure-paragraph)
(if shrface-org
(progn
(insert "** ")
(let ((url (dom-attr (dom-by-tag dom 'a) 'href)))
(shrface-insert-org-link url dom)))
(unless shrface-toggle-bullets
(insert (propertize (concat " " (shrface-bullets-level-string 2) " ") 'face 'shrface-h2-face 'shrface-bullet "shrface-h2-bullet")))
(shrface-shr-fontize-dom dom props face))
(shr-ensure-paragraph))
(defun shrface-shr-h3 (dom props face)
"Insert the Fontized tag h3.
Argument DOM dom.
Argument PROPS text properties.
Argument FACE face."
(shr-ensure-paragraph)
(if shrface-org
(progn
(insert "*** ")
(let ((url (dom-attr (dom-by-tag dom 'a) 'href)))
(shrface-insert-org-link url dom)))
(unless shrface-toggle-bullets
(insert (propertize (concat " " (shrface-bullets-level-string 3) " ") 'face 'shrface-h3-face 'shrface-bullet "shrface-h3-bullet")))
(shrface-shr-fontize-dom dom props face))
(shr-ensure-paragraph))
(defun shrface-shr-h4 (dom props face)
"Insert the Fontized tag h4.
Argument DOM dom.
Argument PROPS text properties.
Argument FACE face."
(shr-ensure-paragraph)
(if shrface-org
(progn
(insert "**** ")
(let ((url (dom-attr (dom-by-tag dom 'a) 'href)))
(shrface-insert-org-link url dom)))
(unless shrface-toggle-bullets
(insert (propertize (concat " " (shrface-bullets-level-string 4) " ") 'face 'shrface-h4-face 'shrface-bullet "shrface-h4-bullet")))
;; (insert (propertize "**** " 'face 'shrface-h4-face))
(shrface-shr-fontize-dom dom props face))
(shr-ensure-paragraph))
(defun shrface-shr-h5 (dom props face)
"Insert the Fontized tag h5.
Argument DOM dom.
Argument PROPS text properties.
Argument FACE face."
(shr-ensure-paragraph)
(if shrface-org
(progn
(insert "***** ")
(let ((url (dom-attr (dom-by-tag dom 'a) 'href)))
(shrface-insert-org-link url dom)))
(unless shrface-toggle-bullets
(insert (propertize (concat " " (shrface-bullets-level-string 5) " ") 'face 'shrface-h5-face 'shrface-bullet "shrface-h5-bullet")) )
(shrface-shr-fontize-dom dom props face))
(shr-ensure-paragraph))
(defun shrface-shr-h6 (dom props face)
"Insert the Fontized tag h6.
Argument DOM dom.
Argument PROPS text properties.
Argument FACE face."
(shr-ensure-paragraph)
(if shrface-org
(progn
(insert "****** ")
(let ((url (dom-attr (dom-by-tag dom 'a) 'href)))
(shrface-insert-org-link url dom)))
(unless shrface-toggle-bullets
(insert (propertize (concat " " (shrface-bullets-level-string 6) " ") 'face 'shrface-h6-face 'shrface-bullet "shrface-h6-bullet")))
;; (insert (propertize "****** " 'face 'shrface-h6-face))
(shrface-shr-fontize-dom dom props face))
(shr-ensure-paragraph))
(defun shrface-tag-code (dom)
"Fontize tag code.
Argument DOM dom."
(if shrface-org
;; TODO not a good way to check if it is inline code
(if (string-match-p "\n" (dom-text dom))
(shr-generic dom)
(insert "~")
(shr-generic dom)
(insert "~"))
(shrface-shr-fontize-dom dom '(comment t) 'shrface-code)))
(defun shrface-tag-figure (dom)
"Fontize tag figure.
Argument DOM dom."
(shr-ensure-newline)
(shrface-shr-fontize-dom dom '(comment t) 'shrface-figure)
(shr-ensure-newline))
(defun shrface-tag-p (dom)
"Fontize tag p.
Argument DOM dom."
(if shrface-org
(progn
(shr-ensure-paragraph)
(let ((url (dom-attr dom 'href)))
(shrface-insert-org-link url dom))
(shr-ensure-paragraph))
(shr-ensure-paragraph)
(shr-generic dom)
(shr-ensure-paragraph)))
(defun shrface-tag-em (dom)
"Fontize tag em.
Argument DOM dom."
(if shrface-org
(progn
(insert "/")
(shr-generic dom)
(insert "/"))
(shrface-shr-fontize-dom dom '(comment t) 'shrface-verbatim)))
(defun shrface-tag-strong (dom)
"Fontize tag strong.
Argument DOM dom."
(if shrface-org
(progn
(insert "*")
(shr-generic dom)
(insert "*"))
(shr-fontize-dom dom 'bold)))
(defun shrface-tag-u (dom)
"Fontize tag u.
Argument DOM dom."
(if shrface-org
(progn
(insert "_")
(shr-generic dom)
(insert "_"))
(shr-fontize-dom dom 'underline)))
(defun shrface-fix-url (url r-url)
"Fix URL based on R-URL."
(if r-url ; r-url is Non-Nil
(progn
(unless (string-match-p "\\`[a-zA-Z][-a-zA-Z0-9+.]*://" r-url) ; fix `r-url' type
(setq r-url (concat "http://" r-url)))
(if url ; url is Non-Nil
(let* ((type (url-type (url-generic-parse-url r-url)))
(host (url-host (url-generic-parse-url r-url)))
(filename (url-filename (url-generic-parse-url r-url)))
(directory (or (file-name-directory filename) "/")))
(if (string-match-p "^/.*" url) ; url start with /, absulute url
(setq url
(format "%s%s%s"
(concat type "://")
(if host
host
(if filename filename "")) ; no host, just use filename
url))
(setq url
(format "%s%s%s"
(concat type "://")
(if host
(concat host directory)
(if filename filename "")) ; no host, just use filename
url))))
"") )
url))
(defun shrface-scheme-p (url)
"Check URL if has scheme or not."
(if (not (url-type (url-generic-parse-url url)))
t))
(defun shrface-tag-svg (dom)
"Fontize tag svg.
Argument DOM dom."
(if shrface-org
nil
(shr-tag-svg dom)))
(defun shrface-tag-img (dom)
"Fontize tag svg.
Argument DOM dom."
(if shrface-org
(let ((url (dom-attr dom 'src)))
(shr-ensure-newline)
(shrface-insert-org-link url dom)
(shr-ensure-newline))
(shr-tag-img dom)))
(defun shrface-tag-pre (dom)
"Fontize tag pre.
Argument DOM dom."
(if shrface-org
(progn
(let ((shr-folding-mode 'none)
(shr-current-font 'default))
(require 'language-detection)
(shr-ensure-newline)
(insert (format "#+BEGIN_SRC %s" (symbol-name
(with-temp-buffer
(shr-generic dom)
(language-detection-buffer)))))
(shr-ensure-newline)
(shr-generic dom)
(shr-ensure-newline)
(insert "#+END_SRC")
(shr-ensure-newline)))
(let ((shr-folding-mode 'none)
(shr-current-font 'default))
(shr-ensure-newline)
(shr-generic dom)
(shr-ensure-newline))))
(defun shrface-tag-title (dom)
"Fontize tag title.
Argument DOM dom."
(if shrface-org
(setq shrface-title
(decode-coding-string
(dom-text dom)
'utf-8 t))
(shr-tag-title dom)))
(defun shrface-tag-span (dom)
"Fontize tag span.
Argument DOM dom."
(if shrface-org
(let ((url (dom-attr dom 'href)))
(shrface-insert-org-link url dom))
(shr-tag-span dom)))
(defun shrface-insert-org-link (url dom)
"TODO: Insert org link based on URL and DOM."
(let* (
(url (if (shrface-scheme-p url)
(shrface-fix-url url shrface-request-url)
url))
(img-dom (dom-by-tag dom 'img))
(img-src (or (dom-attr img-dom 'data-src) ; some sites use data-src as real img data
(dom-attr img-dom 'src)
(let ((srcset (or (dom-attr img-dom 'srcset)
(dom-attr img-dom 'data-srcset))))
(if (stringp srcset)
(car (split-string
(car (split-string srcset ","))
" " )))))) ; get car of src-set, split ',' then split ' '
(img-alt (dom-attr img-dom 'alt))
(img-title (dom-attr img-dom 'title))
(img-width (dom-attr img-dom 'width))
(img-height (dom-attr img-dom 'height)))
(cond
((equal url "") (shr-generic dom))
((equal url nil) (shr-generic dom))
(img-src
(shr-ensure-newline)
(if (not (equal (or img-alt img-title img-src) ""))
(insert (format "#+CAPTION: %s" (or img-alt img-title img-src))))
(shr-ensure-newline)
(let ((width (if (stringp img-width) (string-to-number img-width) 0))
(height (if (stringp img-height) (string-to-number img-height) 0))
(fill-pixels (* (frame-char-width) fill-column)))
(cond ((>= width fill-pixels)
(insert (format "#+ATTR_ORG: :width %s" fill-pixels)))
((and (> width 0) (> height 0))
(insert (format "#+ATTR_ORG: :width %s :height %s" img-width img-height)))
((> width 0)
(insert (format "#+ATTR_ORG: :width %s" img-width)))
((> height 0)
(insert (format "#+ATTR_ORG: :height %s" img-height)))))
(shr-ensure-newline)
(if (shrface-scheme-p img-src)
(if (file-exists-p img-src)
(insert (format "[[./%s]]" img-src)) ; if local images exists, add ./
(insert (format "[[%s]]" (shrface-fix-url img-src shrface-request-url)))) ; if no local images, just fix img-src
(insert (format "[[%s]]" img-src))) ; if not relative path, just orignial img-src
(shr-ensure-newline))
((equal (dom-texts dom) "")
(insert (format "[[%s]]" url)))
((= (length (replace-regexp-in-string "^\\s-*" "" (dom-texts dom))) 0) ;; delete heading whitespaces
(insert (format "[[%s]]" url)))
((> (length (replace-regexp-in-string "^\\s-*" "" (dom-texts dom))) 0) ;; delete heading whitespaces
(insert (format "[[%s][%s]]" url (replace-regexp-in-string "^\\s-*" "" (dom-texts dom)))))
(t (shr-generic dom)))))
(defun shrface-tag-a (dom)
"Fontize tag a.
Argument DOM dom."
(let ((url (dom-attr dom 'href))
(title (dom-attr dom 'title))
(start (point))
shr-start)
(if shrface-org
(shrface-insert-org-link url dom)
(shr-generic dom)
(when (and shr-target-id
(equal (dom-attr dom 'name) shr-target-id))
;; We have a zero-length <a name="foo"> element, so just
;; insert... something.
(when (= start (point))
(shr-ensure-newline)
(insert " "))
(put-text-property start (1+ start) 'shr-target-id shr-target-id))
(when url
(shrface-shr-urlify (or shr-start start) (shr-expand-url url) title)))))
(defun shrface-tag-li (dom)
"Fontize tag li.
Argument DOM dom."
(shr-ensure-newline)
;; (setq shr-indentation 40)
(if shrface-org
(progn
(insert shr-bullet)
(insert " ")
(shr-generic dom))
(let ((start (point)))
(let* ((bullet
(if (numberp shr-list-mode)
(prog1
(format "%d. " shr-list-mode)
(setq shr-list-mode (1+ shr-list-mode)))
(car shr-internal-bullet)))
(width (if (numberp shr-list-mode)
(shr-string-pixel-width bullet)
(cdr shr-internal-bullet))))
(ignore-errors
(if (numberp shr-list-mode)
(insert (propertize bullet 'face 'shrface-item-number-face))
(insert (propertize bullet 'face 'shrface-item-bullet-face))))
(shr-mark-fill start)
(let ((shr-indentation (+ shr-indentation width)))
(put-text-property start (1+ start)
'shr-continuation-indentation shr-indentation)
(put-text-property start (1+ start) 'shr-prefix-length (length bullet))
(shr-generic dom)))))
(unless (bolp)
(insert "\n")))
(defun shrface-tag-dt (dom)
"Fontize tag dt.
Argument DOM dom."
(shr-ensure-newline)
(shrface-shr-fontize-dom dom '(comment t) 'shrface-description-list-term-face)
(shr-ensure-newline))
;;;###autoload
(defun shrface-imenu-get-tree ()
"Produce the index for Imenu."
(dolist (x org-imenu-markers) (move-marker x nil))
(setq org-imenu-markers nil)
(org-with-wide-buffer
(goto-char (point-max))
(let* ((re (shrface-imenu-regexp-bol))
(subs (make-vector (1+ shrface-imenu-depth) nil))
(last-level 0))
(while (re-search-backward re nil t)
;; (message (int-to-string (shrface-level (match-string 1))))
(let ((level (1- (funcall shrface-level)))
(headline (match-string 2)))
(message (int-to-string level ))
(message headline)
;; (when (<= level shrface-imenu-depth)
(when (and (<= level shrface-imenu-depth) (org-string-nw-p headline))
(let* ((m (point-marker))
(item (propertize headline 'org-imenu-marker m 'org-imenu t)))
(message item)
(push m org-imenu-markers)
(if (>= level last-level)
(push (cons item m) (aref subs level))
(push (cons item
(cl-mapcan #'identity (cl-subseq subs (1+ level))))
(aref subs level))
(cl-loop for i from (1+ level) to shrface-imenu-depth
do (aset subs i nil)))
(setq last-level level)))))
(car (cl-remove nil (append subs nil) :test #'eq)))))
(defun shrface-level ()
"Function of no args to compute a header's nesting level in an outline."
(1+ (cl-position (match-string 1) (if (characterp (car shrface-bullets-bullet-list))
(mapcar 'char-to-string shrface-bullets-bullet-list)
shrface-bullets-bullet-list) :test 'equal)))
(defun shrface-regexp ()
"Set regexp for outline minor mode."
(setq-local outline-regexp (shrface-outline-regexp))
(setq-local org-outline-regexp-bol outline-regexp) ; for org-cycle, org-shifttab
(setq-local org-outline-regexp outline-regexp) ; for org-cycle, org-shifttab
(setq-local org-complex-heading-regexp outline-regexp) ; for org-cycle, org-shifttab
(setq-local outline-level shrface-level))
;;;###autoload
(defun shrface-occur ()
"Use `occur' to find all `shrface-tag-h1' to `shrface-tag-h6'.
`shrface-occur' will disable if variable `shrface-toggle-bullets' is Non-nil."
(interactive)
(if (not shrface-toggle-bullets)
(occur (shrface-outline-regexp))
(message "Please set `shrface-toggle-bullets' nil to use `shrface-occur'")))
(defun shrface-occur-flash ()
"Flash the occurrence line."
(shrface-flash-show (line-beginning-position) (line-end-position) 'shrface-highlight 0.5)
(overlay-put compilation-highlight-overlay 'window (selected-window)))
;;;###autoload
(define-minor-mode shrface-mode
"Toggle shr minor mode.
1. imenu
2. outline-minor-mode"
:group 'shrface
(cond
(shrface-mode
(shrface-basic)
(shrface-trial)
(unless shrface-toggle-bullets
(shrface-regexp)
(setq imenu-create-index-function #'shrface-imenu-get-tree)
(outline-minor-mode))
(run-hooks 'shrface-mode-hook))
(t
(shrface-resume)
(setq shr-bullet "* ")
(setq imenu-create-index-function nil)
(outline-minor-mode -1))))
(defun shrface-basic()
"Enable the shrface faces.
Need to be called once before loading eww, nov.el, dash-docs, mu4e, after shr."
(interactive)
(shrface-shr-item-bullet)
(dolist (sub shrface-supported-faces-alist)
(unless (member sub shr-external-rendering-functions)
(add-to-list 'shr-external-rendering-functions sub)))
(if (boundp 'nov-shr-rendering-functions)
(setq nov-shr-rendering-functions '((img . nov-render-img) (title . nov-render-title))))
(if (boundp 'nov-shr-rendering-functions)
(setq nov-shr-rendering-functions (append nov-shr-rendering-functions shr-external-rendering-functions)))
;; setup occur flash
(add-hook 'occur-mode-find-occurrence-hook #'shrface-occur-flash)
;; setup `shrface-links-counsel' ivy actions
(shrface-links-counsel-set-actions)