forked from masasam/emacs-easy-hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy-hugo.el
2519 lines (2329 loc) · 93.5 KB
/
easy-hugo.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
;;; easy-hugo.el --- Write blogs made with hugo by markdown or org-mode -*- lexical-binding: t; -*-
;; Copyright (C) 2017-2024 by Masashi Miyaura
;; Author: Masashi Miyaura
;; URL: https://github.com/masasam/emacs-easy-hugo
;; Version: 3.10.60
;; Package-Requires: ((emacs "25.1") (popup "0.5.3") (request "0.3.0") (transient "0.3.6"))
;; 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:
;; Emacs major mode for writing blogs made with hugo
;; by markdown or org-mode or AsciiDoc or reStructuredText or mmark or html.
;; You can publish your blog to the server or Github Pages
;; or Amazon S3 or Google Cloud Storage.
;;; Code:
(require 'cl-lib)
(require 'url)
(require 'request)
(defgroup easy-hugo nil
"Writing blogs made with hugo."
:group 'tools)
(defgroup easy-hugo-faces nil
"Faces used in `easy-hugo'"
:group 'easy-hugo :group 'faces)
(defcustom easy-hugo-basedir nil
"Directory where hugo html source code is placed."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-bin "hugo"
"Hugo binary."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-server-flags ""
"Additional flags to pass to hugo server."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-server-flags2 ""
"Additional flags to pass to hugo server."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-rsync-flags "-rtpl"
"Additional flags for rsync."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-server-value ""
"Additional value to pass to hugo server."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-server-value2 ""
"Additional value to pass to hugo server."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-preview-url "http://localhost:1313/"
"Preview url of `easy-hugo'."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-url nil
"Url of the site operated by hugo."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-sshdomain nil
"Domain of hugo at your ~/.ssh/config."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-root nil
"Root directory of hugo at your server."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-previewtime 300
"Preview display time."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-image-directory "images"
"Image file directory under 'static' directory."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-default-picture-directory "~"
"Default directory for selecting images with `easy-hugo-put-image'."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-amazon-s3-bucket-name nil
"Amazon S3 bucket name."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-google-cloud-storage-bucket-name nil
"Google Cloud Storage bucket name."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-default-ext ".md"
"Default extension when posting new articles."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-helm-ag nil
"Helm-ag use flg."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-no-help nil
"No help flg of `easy-hugo'."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-emacspeak nil
"Emacspeak flg of `easy-hugo'."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-additional-help nil
"Additional help flg of `easy-hugo'."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-sort-default-char nil
"Default setting to sort with charactor."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-publish-chmod "Du=rwx,Dgo=rx,Fu=rw,Fog=r"
"Permission when publish.
The default is drwxr-xr-x."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-github-deploy-script "deploy.sh"
"Github-deploy-script file name."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-markdown-extension "md"
"Markdown extension.
Please select md or markdown or mdown.
Because only these three are supported by hugo."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-asciidoc-extension "ad"
"Asciidoc extension.
Please select ad or asciidoc or adoc.
Because only these three are supported by hugo."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-html-extension "html"
"Html extension.
Please select html or htm.
Because only two are supported by hugo."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-postdir "content/post"
"Directory where the theme stores its posts."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-rsync-delete-directory "public/"
"Disappear directory when synchronizing with rsync."
:group 'easy-hugo
:type 'string)
(defcustom easy-hugo-help-line 7
"Number of lines of `easy-hugo-help'."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-add-help-line 6
"Number of lines of `easy-hugo-add-help'."
:group 'easy-hugo
:type 'integer)
(defcustom easy-hugo-org-header nil
"Flg of use in org format header with hugo version 0.25 and above."
:group 'easy-hugo
:type 'integer)
(defvar easy-hugo--preview-loop t
"Preview loop flg.")
(defvar easy-hugo--server-process nil
"Hugo process.")
(if easy-hugo-no-help
(defvar easy-hugo--unmovable-line 3
"Impossible to move below this line.")
(defvar easy-hugo--unmovable-line (+ easy-hugo-help-line 4)
"Impossible to move below this line."))
(defvar easy-hugo--draft-list nil
"Draft list flg.")
(defvar easy-hugo--draft-mode nil
"Display draft-mode.")
(defvar easy-hugo--current-postdir 0
"Easy-hugo current postdir.")
(defvar easy-hugo--postdir-list nil
"Easy-hugo postdir list.")
(defconst easy-hugo--unmovable-line-default (+ easy-hugo-help-line 4)
"Default value of impossible to move below this line.")
(defconst easy-hugo--preview-buffer "*Hugo Preview*"
"Easy-hugo preview buffer name.")
(defconst easy-hugo--formats `(,easy-hugo-markdown-extension
"org"
,easy-hugo-asciidoc-extension
"rst"
"mmark"
,easy-hugo-html-extension))
(defface easy-hugo-help-face
`((((class color) (background light))
,@(and (>= emacs-major-version 27) '(:extend t))
:bold t
:foreground "#82c600"
:background "#f0f8ff")
(((class color) (background dark))
,@(and (>= emacs-major-version 27) '(:extend t))
:bold t
:foreground "#82c600"
:background "#2f4f4f"))
"Definition of help color."
:group 'easy-hugo-faces)
(defvar easy-hugo--mode-buffer nil
"Main buffer of `easy-hugo'.")
(defvar easy-hugo--cursor nil
"Cursor of `easy-hugo'.")
(defvar easy-hugo--line nil
"Line of `easy-hugo'.")
(defvar easy-hugo--sort-time-flg 1
"Sort time flg of `easy-hugo'.")
(defvar easy-hugo--sort-char-flg nil
"Sort char flg of `easy-hugo'.")
(defvar easy-hugo--sort-publishday-flg nil
"Sort publishtime flg of `easy-hugo'.")
(defvar easy-hugo--refresh nil
"Refresh flg of `easy-hugo'.")
(defvar easy-hugo--current-blog 0
"Current blog number.")
(defcustom easy-hugo-bloglist nil
"Multiple blog setting."
:group 'easy-hugo
:type 'string)
(push `((easy-hugo-basedir . ,easy-hugo-basedir)
(easy-hugo-bin . ,easy-hugo-bin)
(easy-hugo-url . ,easy-hugo-url)
(easy-hugo-root . ,easy-hugo-root)
(easy-hugo-sshdomain . ,easy-hugo-sshdomain)
(easy-hugo-amazon-s3-bucket-name . ,easy-hugo-amazon-s3-bucket-name)
(easy-hugo-google-cloud-storage-bucket-name . ,easy-hugo-google-cloud-storage-bucket-name)
(easy-hugo-github-deploy-script . ,easy-hugo-github-deploy-script)
(easy-hugo-image-directory . ,easy-hugo-image-directory)
(easy-hugo-default-picture-directory . ,easy-hugo-default-picture-directory)
(easy-hugo-postdir . ,easy-hugo-postdir)
(easy-hugo-publish-chmod . ,easy-hugo-publish-chmod)
(easy-hugo-previewtime . ,easy-hugo-previewtime)
(easy-hugo-preview-url . ,easy-hugo-preview-url)
(easy-hugo-sort-default-char . ,easy-hugo-sort-default-char)
(easy-hugo-asciidoc-extension . ,easy-hugo-asciidoc-extension)
(easy-hugo-html-extension . ,easy-hugo-html-extension)
(easy-hugo-markdown-extension . ,easy-hugo-markdown-extension)
(easy-hugo-default-ext . ,easy-hugo-default-ext))
easy-hugo-bloglist)
(defvar easy-hugo--publish-timer-list
(make-list (length easy-hugo-bloglist) 'nil)
"Timer list for cancel publish timer.")
(defvar easy-hugo--firebase-deploy-timer-list
(make-list (length easy-hugo-bloglist) 'nil)
"Timer list for cancel firebase deploy timer.")
(defvar easy-hugo--github-deploy-timer-list
(make-list (length easy-hugo-bloglist) 'nil)
"Timer list for cancel github deploy timer.")
(defvar easy-hugo--amazon-s3-deploy-timer-list
(make-list (length easy-hugo-bloglist) 'nil)
"Timer list for cancel amazon s3 deploy timer.")
(defvar easy-hugo--google-cloud-storage-deploy-timer-list
(make-list (length easy-hugo-bloglist) 'nil)
"Timer list for cancel google cloud storage deploy timer.")
(defconst easy-hugo--default-bin
"hugo"
"Default easy-hugo-bin.")
(defconst easy-hugo--default-github-deploy-script
"deploy.sh"
"Default `easy-hugo' github-deploy-script.")
(defconst easy-hugo--default-image-directory
"images"
"Default `easy-hugo' image-directory.")
(defconst easy-hugo--default-picture-directory
"~"
"Default `easy-hugo' picture-directory.")
(defconst easy-hugo--default-publish-chmod
"Du=rwx,Dgo=rx,Fu=rw,Fog=r"
"Default `easy-hugo' publish-chmod.")
(defconst easy-hugo--default-previewtime
300
"Default `easy-hugo' previewtime.")
(defconst easy-hugo--default-preview-url
"http://localhost:1313/"
"Default `easy-hugo' preview-url.")
(defconst easy-hugo--default-sort-default-char
nil
"Default `easy-hugo' sort-default-char.")
(defconst easy-hugo--default-asciidoc-extension
"ad"
"Default `easy-hugo' asciidoc-extension.")
(defconst easy-hugo--default-html-extension
"html"
"Default `easy-hugo' html-extension.")
(defconst easy-hugo--default-markdown-extension
"md"
"Default `easy-hugo' markdown-extension.")
(defconst easy-hugo--default-postdir
"content/post"
"Default `easy-hugo-postdir'.")
(defconst easy-hugo--default-ext
easy-hugo-default-ext
"Default `easy-hugo' default-ext.")
(defconst easy-hugo--buffer-name "*Easy-hugo*"
"Buffer name of `easy-hugo'.")
(defconst easy-hugo--forward-char 20
"Forward-char of `easy-hugo'.")
(defmacro easy-hugo-with-env (&rest body)
"Evaluate BODY with `default-directory' set to `easy-hugo-basedir'.
Report an error if hugo is not installed, or if `easy-hugo-basedir' is unset."
`(progn
(unless easy-hugo-basedir
(error "Please set easy-hugo-basedir variable"))
(unless (executable-find easy-hugo-bin)
(error "'hugo' is not installed"))
(let ((default-directory easy-hugo-basedir))
,@body)))
(defmacro easy-hugo-set-bloglist (body)
"Macros to set variables to `easy-hugo-bloglist' as BODY."
`(setq ,body
(cdr (assoc ',body
(nth easy-hugo--current-blog easy-hugo-bloglist)))))
(defmacro easy-hugo-eval-bloglist (body)
"Macros to eval variables of BODY from `easy-hugo-bloglist'."
`(cdr (assoc ',body
(nth easy-hugo--current-blog easy-hugo-bloglist))))
(defmacro easy-hugo-nth-eval-bloglist (body blog)
"Macros to eval variables of BODY from `easy-hugo-bloglist' at BLOG."
`(cdr (assoc ',body
(nth ,blog easy-hugo-bloglist))))
(defmacro easy-hugo-ignore-error (condition &rest body)
"Execute BODY; if the error CONDITION occurs, return nil.
Otherwise, return result of last form in BODY.
CONDITION can also be a list of error conditions."
(declare (debug t) (indent 1))
`(condition-case nil (progn ,@body) (,condition nil)))
;;;###autoload
(defun easy-hugo-article ()
"Open a list of articles written in hugo with dired."
(interactive)
(unless easy-hugo-basedir
(error "Please set easy-hugo-basedir variable"))
(find-file (expand-file-name easy-hugo-postdir easy-hugo-basedir)))
;;;###autoload
(defun easy-hugo-magit ()
"Open magit at current blog."
(interactive)
(unless easy-hugo-basedir
(error "Please set easy-hugo-basedir variable"))
(if (require 'magit nil t)
(magit-status-setup-buffer easy-hugo-basedir)
(error "'magit' is not installed")))
(defun easy-hugo-emacspeak-filename ()
"Read filename with emacspeak."
(cl-declare (special emacspeak-speak-last-spoken-word-position))
(let ((filename (substring (thing-at-point 'line) easy-hugo--forward-char -1))
(personality (dtk-get-style)))
(cond
(filename
(dtk-speak (propertize filename 'personality personality))
(setq emacspeak-speak-last-spoken-word-position (point)))
(t (emacspeak-speak-line)))))
;;;###autoload
(defun easy-hugo-image ()
"Generate image link."
(interactive
(easy-hugo-with-env
(unless (file-directory-p (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))
(error "%s does not exist" (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))))
(let ((insert-default-directory nil))
(let ((file (read-file-name "Image file: " nil
(expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))
t
(expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))))
(insert (concat (format "{{< figure src=\"%s%s\""
easy-hugo-url
(concat
"/"
easy-hugo-image-directory
"/"
(file-name-nondirectory file)))
" alt=\"\" >}}")))))))
;;;###autoload
(defun easy-hugo-figure ()
"Generate figure shortcode."
(interactive
(easy-hugo-with-env
(unless (file-directory-p (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))
(error "%s does not exist" (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))))
(let ((insert-default-directory nil))
(let ((file (read-file-name "Image file: " nil
(expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))
t
(expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))))
(insert (concat (format "{{< figure src=\"%s%s\""
easy-hugo-url
(concat
"/"
easy-hugo-image-directory
"/"
(file-name-nondirectory file)))
" title=\"\" >}}")))))))
;;;###autoload
(defun easy-hugo-put-image ()
"Move image to image directory and generate image link."
(interactive
(easy-hugo-with-env
(unless (file-directory-p (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))
(error "%s does not exist" (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))))
(let ((insert-default-directory nil))
(let* ((file (read-file-name "Image file: " nil
(expand-file-name easy-hugo-default-picture-directory)
t
(expand-file-name easy-hugo-default-picture-directory)))
(putfile (expand-file-name
(file-name-nondirectory file)
(expand-file-name easy-hugo-image-directory "static"))))
(when (file-exists-p putfile)
(error "%s already exists!" putfile))
(copy-file file putfile)
(insert (concat (format "{{< figure src=\"%s%s\""
easy-hugo-url
(concat
"/"
easy-hugo-image-directory
"/"
(file-name-nondirectory file)))
" alt=\"\" >}}")))))))
;;;###autoload
(defun easy-hugo-put-figure ()
"Move image to image directory and generate figure shortcode."
(interactive
(easy-hugo-with-env
(unless (file-directory-p (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))
(error "%s does not exist" (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))))
(let ((insert-default-directory nil))
(let* ((file (read-file-name "Image file: " nil
(expand-file-name easy-hugo-default-picture-directory)
t
(expand-file-name easy-hugo-default-picture-directory)))
(putfile (expand-file-name
(file-name-nondirectory file)
(expand-file-name easy-hugo-image-directory "static"))))
(when (file-exists-p putfile)
(error "%s already exists!" putfile))
(copy-file file putfile)
(insert (concat (format "{{< figure src=\"%s%s\""
easy-hugo-url
(concat
"/"
easy-hugo-image-directory
"/"
(file-name-nondirectory file)))
" title=\"\" >}}")))))))
(defun easy-hugo--request-image (url file)
"Resuest image from URL and save file at the location of FILE."
(request
url
:parser 'buffer-string
:success
(cl-function (lambda (&key data &allow-other-keys)
(when data
(with-current-buffer (get-buffer-create "*request image*")
(erase-buffer)
(insert data)
(write-file file)))))
:error
(cl-function (lambda (&rest args &key error-thrown &allow-other-keys)
(message "Got error: %S" error-thrown)))))
;;;###autoload
(defun easy-hugo-pull-image ()
"Pull image from internet to image directory and generate image link."
(interactive
(easy-hugo-with-env
(unless (file-directory-p (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))
(error "%s does not exist" (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))))
(let ((url (read-string "URL: " (if (fboundp 'gui-get-selection)
(gui-get-selection))))
(file (read-file-name "Save as: "
(expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))
(car (last (split-string
(substring-no-properties (gui-get-selection))
"/")))
nil)))
(when (file-exists-p (file-truename file))
(error "%s already exists!" (file-truename file)))
(easy-hugo--request-image url file)
(insert (concat (format "{{< figure src=\"%s%s\""
easy-hugo-url
(concat
"/"
easy-hugo-image-directory
"/"
(file-name-nondirectory file)))
" alt=\"\" >}}"))))))
;;;###autoload
(defun easy-hugo-pull-figure ()
"Pull image from internet to image directory and generate figure shortcode."
(interactive
(easy-hugo-with-env
(unless (file-directory-p (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir)))
(error "%s does not exist" (expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))))
(let ((url (read-string "URL: " (if (fboundp 'gui-get-selection)
(gui-get-selection))))
(file (read-file-name "Save as: "
(expand-file-name
easy-hugo-image-directory
(expand-file-name "static" easy-hugo-basedir))
(car (last (split-string
(substring-no-properties (gui-get-selection))
"/")))
nil)))
(when (file-exists-p (file-truename file))
(error "%s already exists!" (file-truename file)))
(easy-hugo--request-image url file)
(insert (concat (format "{{< figure src=\"%s%s\""
easy-hugo-url
(concat
"/"
easy-hugo-image-directory
"/"
(file-name-nondirectory file)))
" title=\"\" >}}"))))))
;;;###autoload
(defun easy-hugo-publish-clever ()
"Clever publish command.
Automatically select the deployment destination from init.el."
(interactive)
(easy-hugo-with-env
(cond ((easy-hugo-eval-bloglist easy-hugo-root)
(easy-hugo-publish))
((easy-hugo-eval-bloglist easy-hugo-amazon-s3-bucket-name)
(easy-hugo-amazon-s3-deploy))
((easy-hugo-eval-bloglist easy-hugo-google-cloud-storage-bucket-name)
(easy-hugo-google-cloud-storage-deploy))
((executable-find (expand-file-name
(if (easy-hugo-eval-bloglist easy-hugo-github-deploy-script)
(easy-hugo-eval-bloglist easy-hugo-github-deploy-script)
easy-hugo-github-deploy-script)
easy-hugo-basedir))
(easy-hugo-github-deploy))
((executable-find "firebase")
(easy-hugo-firebase-deploy))
(t (error "Nothing is found to publish at %s" easy-hugo-basedir)))))
;;;###autoload
(defun easy-hugo-publish ()
"Adapt local change to the server with hugo."
(interactive)
(unless easy-hugo-sshdomain
(error "Please set easy-hugo-sshdomain variable"))
(unless easy-hugo-root
(error "Please set easy-hugo-root variable"))
(unless (executable-find "rsync")
(error "'rsync' is not installed"))
(unless (file-exists-p "~/.ssh/config")
(error "There is no ~/.ssh/config"))
(easy-hugo-with-env
(when (file-directory-p "public")
(delete-directory "public" t nil))
(let ((ret (call-process easy-hugo-bin nil "*hugo-publish*" t "--destination" "public")))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-publish*"))
(error "'hugo --destination public' command does not end normally")))
(when (get-buffer "*hugo-publish*")
(kill-buffer "*hugo-publish*"))
(let ((ret (call-process "rsync"
nil
"*hugo-rsync*"
t
easy-hugo-rsync-flags
(concat "--chmod=" easy-hugo-publish-chmod)
"--delete"
easy-hugo-rsync-delete-directory
(concat easy-hugo-sshdomain ":" (shell-quote-argument easy-hugo-root)))))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-rsync*"))
(error "'rsync' command does not end normally")))
(when (get-buffer "*hugo-rsync*")
(kill-buffer "*hugo-rsync*"))
(message "Blog published")
(when easy-hugo-url
(browse-url easy-hugo-url))))
;;;###autoload
(defun easy-hugo-publish-timer (n)
"A timer that publish after the N number of minutes has elapsed."
(interactive "nMinute:")
(unless easy-hugo-basedir
(error "Please set easy-hugo-basedir variable"))
(unless (executable-find easy-hugo-bin)
(error "'hugo' is not installed"))
(unless easy-hugo-sshdomain
(error "Please set easy-hugo-sshdomain variable"))
(unless easy-hugo-root
(error "Please set easy-hugo-root variable"))
(unless (executable-find "rsync")
(error "'rsync' is not installed"))
(unless (file-exists-p "~/.ssh/config")
(error "There is no ~/.ssh/config"))
(let ((blognum easy-hugo--current-blog))
(if (nth blognum easy-hugo--publish-timer-list)
(message "There is already reserved publish-timer on %s" easy-hugo-url)
(setf (nth easy-hugo--current-blog easy-hugo--publish-timer-list)
(run-at-time (* n 60) nil
#'(lambda () (easy-hugo-publish-on-timer blognum)))))))
;;;###autoload
(defun easy-hugo-cancel-publish-timer ()
"Cancel timer that publish after the specified number of minutes has elapsed."
(interactive)
(if (nth easy-hugo--current-blog easy-hugo--publish-timer-list)
(progn
(cancel-timer (nth easy-hugo--current-blog easy-hugo--publish-timer-list))
(setf (nth easy-hugo--current-blog easy-hugo--publish-timer-list) nil)
(message "Publish-timer canceled on %s" easy-hugo-url))
(message "There is no reserved publish-timer on %s" easy-hugo-url)))
(defun easy-hugo-publish-on-timer (n)
"Adapt local change to the server with hugo on timer at N."
(let ((default-directory (easy-hugo-nth-eval-bloglist easy-hugo-basedir n)))
(when (file-directory-p "public")
(delete-directory "public" t nil))
(let ((ret (call-process easy-hugo-bin nil "*hugo-publish*" t "--destination" "public")))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-publish*"))
(setf (nth n easy-hugo--publish-timer-list) nil)
(error "'hugo --destination public' command does not end normally")))
(when (get-buffer "*hugo-publish*")
(kill-buffer "*hugo-publish*"))
(let ((ret (call-process "rsync"
nil
"*hugo-rsync*"
t
easy-hugo-rsync-flags
(concat "--chmod=" easy-hugo-publish-chmod)
"--delete"
easy-hugo-rsync-delete-directory
(concat
(easy-hugo-nth-eval-bloglist easy-hugo-sshdomain n)
":"
(shell-quote-argument (easy-hugo-nth-eval-bloglist easy-hugo-root n))))))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-rsync*"))
(error "'rsync' command does not end normally")))
(when (get-buffer "*hugo-rsync*")
(kill-buffer "*hugo-rsync*"))
(message "Blog published")
(when (easy-hugo-nth-eval-bloglist easy-hugo-url n)
(browse-url (easy-hugo-nth-eval-bloglist easy-hugo-url n)))
(setf (nth n easy-hugo--publish-timer-list) nil)))
;;;###autoload
(defun easy-hugo-firebase-deploy ()
"Deploy hugo at firebase."
(interactive)
(unless (executable-find "firebase")
(error "'firebase-tools' is not installed"))
(easy-hugo-with-env
(when (file-directory-p "public")
(delete-directory "public" t nil))
(let ((ret (call-process easy-hugo-bin nil "*hugo-publish*" t "--destination" "public")))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-publish*"))
(error "'hugo --destination public' command does not end normally")))
(when (get-buffer "*hugo-publish*")
(kill-buffer "*hugo-publish*"))
(let ((ret (call-process "firebase"
nil
"*hugo-firebase*"
t
"deploy")))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-firebase*"))
(error "'firebase deploy' command does not end normally")))
(when (get-buffer "*hugo-firebase*")
(kill-buffer "*hugo-firebase*"))
(message "Blog published")
(when easy-hugo-url
(browse-url easy-hugo-url))))
;;;###autoload
(defun easy-hugo-firebase-deploy-timer (n)
"A timer that firebase deploy after the N number of minutes has elapsed."
(interactive "nMinute:")
(unless easy-hugo-basedir
(error "Please set easy-hugo-basedir variable"))
(unless (executable-find easy-hugo-bin)
(error "'hugo' is not installed"))
(unless (executable-find "firebase")
(error "'firebase-tools' is not installed"))
(let ((blognum easy-hugo--current-blog))
(if (nth blognum easy-hugo--firebase-deploy-timer-list)
(message "There is already reserved firebase-deploy-timer on %s" easy-hugo-url)
(setf (nth easy-hugo--current-blog easy-hugo--firebase-deploy-timer-list)
(run-at-time (* n 60) nil
#'(lambda () (easy-hugo-firebase-deploy-on-timer blognum)))))))
;;;###autoload
(defun easy-hugo-cancel-firebase-deploy-timer ()
"Cancel timer that firebase deploy after the specified number of minutes has elapsed."
(interactive)
(if (nth easy-hugo--current-blog easy-hugo--firebase-deploy-timer-list)
(progn
(cancel-timer (nth easy-hugo--current-blog easy-hugo--firebase-deploy-timer-list))
(setf (nth easy-hugo--current-blog easy-hugo--firebase-deploy-timer-list) nil)
(message "Firebase-deploy-timer canceled on %s" easy-hugo-url))
(message "There is no reserved firebase-deploy-timer on %s" easy-hugo-url)))
(defun easy-hugo-firebase-deploy-on-timer (n)
"Deploy hugo at firebase on timer at N."
(let ((default-directory (easy-hugo-nth-eval-bloglist easy-hugo-basedir n)))
(when (file-directory-p "public")
(delete-directory "public" t nil))
(let ((ret (call-process easy-hugo-bin nil "*hugo-firebase*" t "--destination" "public")))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-firebase*"))
(setf (nth n easy-hugo--firebase-deploy-timer-list) nil)
(error "'hugo --destination public' command does not end normally")))
(when (get-buffer "*hugo-firebase*")
(kill-buffer "*hugo-firebase*"))
(let ((ret (call-process "firebase"
nil
"*hugo-firebase*"
t
"deploy")))
(unless (zerop ret)
(switch-to-buffer (get-buffer "*hugo-firebase*"))
(error "'firebase deploy' command does not end normally")))
(when (get-buffer "*hugo-firebase*")
(kill-buffer "*hugo-firebase*"))
(message "Blog published")
(when (easy-hugo-nth-eval-bloglist easy-hugo-url n)
(browse-url (easy-hugo-nth-eval-bloglist easy-hugo-url n)))
(setf (nth n easy-hugo--firebase-deploy-timer-list) nil)))
(defun easy-hugo--org-headers (file)
"Return a draft org mode header string for a new article as FILE."
(let ((datetimezone
(concat
(format-time-string "%Y-%m-%dT%T")
(easy-hugo--orgtime-format (format-time-string "%z")))))
(concat
"#+TITLE: " file
"\n#+DATE: " datetimezone
"\n#+DRAFT: nil"
"\n#+CATEGORIES[]: nil nil"
"\n#+TAGS[]: nil nil"
"\n#+DESCRIPTION: Short description"
"\n\n")))
;;;###autoload
(defun easy-hugo-newpost (post-file)
"Create a new post with hugo.
POST-FILE needs to have and extension '.md' or '.org' or '.ad' or '.rst' or '.mmark' or '.html'."
(interactive (list (read-from-minibuffer
"Filename: "
`(,easy-hugo-default-ext . 1) nil nil nil)))
(easy-hugo-with-env
(let ((filename (expand-file-name post-file easy-hugo-postdir))
(file-ext (file-name-extension post-file)))
(when (not (member file-ext easy-hugo--formats))
(error "Please enter .%s or .org or .%s or .rst or .mmark or .%s file name"
easy-hugo-markdown-extension
easy-hugo-asciidoc-extension
easy-hugo-html-extension))
(when (file-exists-p (file-truename filename))
(error "%s already exists!" filename))
(if (null easy-hugo-org-header)
(call-process easy-hugo-bin nil "*hugo*" t "new"
(file-relative-name filename
(expand-file-name "content" easy-hugo-basedir)))
(progn
(if (or (string-equal file-ext easy-hugo-markdown-extension)
(string-equal file-ext easy-hugo-asciidoc-extension)
(string-equal file-ext "rst")
(string-equal file-ext "mmark")
(string-equal file-ext easy-hugo-html-extension))
(call-process easy-hugo-bin nil "*hugo*" t "new"
(file-relative-name filename
(expand-file-name "content" easy-hugo-basedir))))))
(when (get-buffer "*hugo*")
(kill-buffer "*hugo*"))
(find-file filename)
(when (and easy-hugo-org-header
(string-equal file-ext "org"))
(insert (easy-hugo--org-headers (file-name-base post-file))))
(goto-char (point-max))
(save-buffer))))
;;;###autoload
(defun easy-hugo-preview ()
"Preview hugo at localhost."
(interactive)
(easy-hugo-with-env
(if (process-live-p easy-hugo--server-process)
(easy-hugo--preview-open)
(progn
(setq easy-hugo--server-process
(start-process "hugo-server"
easy-hugo--preview-buffer easy-hugo-bin "server" "--navigateToChanged" easy-hugo-server-flags easy-hugo-server-value easy-hugo-server-flags2 easy-hugo-server-value2))
(while easy-hugo--preview-loop
(if (equal (easy-hugo--preview-status easy-hugo-preview-url) "200")
(progn
(setq easy-hugo--preview-loop nil)
(easy-hugo--preview-open)))
(sleep-for 0 100)
(if (and (eq (process-status easy-hugo--server-process) 'exit)
(equal (process-exit-status easy-hugo--server-process) 255))
(progn
(switch-to-buffer easy-hugo--preview-buffer)
(error "Hugo error look at %s buffer" easy-hugo--preview-buffer))))
(setq easy-hugo--preview-loop t)
(run-at-time easy-hugo-previewtime nil 'easy-hugo--preview-end)))))
(defun easy-hugo--preview-open ()
"Open preview at the file name on the pointer.
If not applicable, return the default preview."
(if (equal (buffer-name (current-buffer)) easy-hugo--buffer-name)
(if (not (or (string-match "^$" (thing-at-point 'line))
(eq (point) (point-max))
(> (+ 1 easy-hugo--forward-char) (length (thing-at-point 'line)))))
(progn
(let ((file (expand-file-name
(substring (thing-at-point 'line) easy-hugo--forward-char -1)
easy-hugo-postdir)))
(when (and (file-exists-p file)
(not (file-directory-p file)))
(if (equal (easy-hugo--preview-http-status-code
(file-name-sans-extension
(file-relative-name file
(expand-file-name
"content"
easy-hugo-basedir))))
"200")
(browse-url (concat easy-hugo-preview-url
(file-name-sans-extension
(file-relative-name file
(expand-file-name
"content"
easy-hugo-basedir)))))
(browse-url easy-hugo-preview-url)))))
(browse-url easy-hugo-preview-url))
(if buffer-file-name
(if (equal (easy-hugo--preview-http-status-code
(file-name-sans-extension
(file-relative-name (file-truename buffer-file-name)
(expand-file-name "content" easy-hugo-basedir))))
"200")
(browse-url (concat easy-hugo-preview-url
(file-name-sans-extension
(file-relative-name (file-truename buffer-file-name)
(expand-file-name "content" easy-hugo-basedir)))))
(browse-url easy-hugo-preview-url))
(browse-url easy-hugo-preview-url))))
(defun easy-hugo--preview-http-status-code (url)
"Return the http status code of the preview URL."
(nth 1
(split-string
(nth 0
(split-string
(with-current-buffer
(easy-hugo--url-retrieve-synchronously (concat easy-hugo-preview-url url))
(prog1
(buffer-string)
(kill-buffer)))