-
Notifications
You must be signed in to change notification settings - Fork 32
/
org-wiki.el
1675 lines (1401 loc) · 57.4 KB
/
org-wiki.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-wiki.el --- Desktop wiki extension for org-mode -*- lexical-binding: t; -*-
;; Public Domain Software - Do whatever you want, use at will.
;; Author: Caio Rodrigues <caiorss DOT rodrigues AT gmail DOT com>
;; Maintainer: Caio Rordrigues <caiorss DOT rodrigues AT gmail DOT com>
;; Keywords: org-mode, wiki, notes, notebook
;; Version: 5.1
;; URL: https://www.github.com/caiorss/org-wiki'
;; Package-Requires: ((helm-core "2.0") (cl-lib "0.5"))
;; This is free and unencumbered software released into the public domain.
;;
;; Anyone is free to copy, modify, publish, use, compile, sell, or
;; distribute this software, either in source code form or as a compiled
;; binary, for any purpose, commercial or non-commercial, and by any
;; means.
;;
;; In jurisdictions that recognize copyright laws, the author or authors
;; of this software dedicate any and all copyright interest in the
;; software to the public domain. We make this dedication for the benefit
;; of the public at large and to the detriment of our heirs and
;; successors. We intend this dedication to be an overt act of
;; relinquishment in perpetuity of all present and future rights to this
;; software under copyright law.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;; IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
;; OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
;; ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
;; OTHER DEALINGS IN THE SOFTWARE.
;;
;; For more information, please refer to <http://unlicense.org/>
;;; Commentary:
;; Org-wiki is a org-mode extension that provides tools to manage and
;; build a desktop wiki where each wiki page is an org-mode file.
;;
;;; Code:
;; external libraries
(require 'ox-html)
(require 'helm)
;; built-in Emacs lib
(require 'cl-lib) ;; Common-lisp emulation library
(require 'easymenu)
(require 'subr-x) ;; Provides string trim functions.
;; ****************** U S E R - S E T T I N G S ************************ ;;
(defgroup org-wiki nil
"Org-wiki Settings"
:group 'tools
)
(defcustom org-wiki-location-list '("~/org/wiki")
"List of org-wiki root directories"
:type '(repeat directory)
:group 'org-wiki
)
(defvar org-wiki-location nil)
(defcustom org-wiki-default-read-only nil
"If this variable is true all org-wiki pages will open as read-only by default.
You can toggle read-only mode with M-x read-only-mode or C-x C-q."
:type 'boolean
:group 'org-wiki
)
(defcustom org-wiki-close-root-switch t
"If this variable is true, all org-wiki pages are closed when root directory is switched.
(Default value: true)"
:type 'boolean
:group 'org-wiki
)
;;; ======= Python Webserver Settings =========== ;;
(defcustom org-wiki-server-port "8000"
"Default port to server org-wiki static files server."
:type 'string
:group 'org-wiki
)
(defcustom org-wiki-server-host "0.0.0.0"
"Default address that the server listens to."
:type 'string
:group 'org-wiki
)
;; ======== Async export settings ================ ;;
(defcustom org-wiki-emacs-path "emacs"
"Path to Emacs executable. Default value 'emacs'."
:type 'file
:group 'org-wiki
)
;;; Path to init file like init.el used by function org-wiki-html-export
;; The user can set for a more lightweight file in order to speed up the
;; exporting speed.
;;
(defcustom org-wiki-user-init-file (concat (file-name-as-directory user-emacs-directory) "init.el")
"Path to init.el file used for asynchronous export."
:type 'file
:group 'org-wiki
)
;; ====== Optional Clip.jar image pasting app =========== ;;
(defcustom org-wiki-backup-location nil
"Path to backup directory."
:type 'directory
:group 'org-wiki
)
;; Optional Clip.jar image pasting app
(defcustom org-wiki-clip-jar-path "~/bin/Clip.jar"
"Path to Clip.jar utility to paste images from clipboard."
:type 'file
:group 'org-wiki
)
;;; Default index page (index.org) accessed with M-x org-wiki-index
;;
(defvar org-wiki-index-file-basename "index")
;;; Additional publishing options
(defcustom org-wiki-publish-plist '()
"Additional options passed to `org-publish'."
:type 'plist
:group 'org-wiki)
(defcustom org-wiki-template
(concat "#+TITLE: %n\n"
"#+DESCRIPTION:\n"
"#+KEYWORDS:\n"
"#+STARTUP: content\n"
"\n\n"
"- [[wiki:index][Index]]\n\n"
"- Related: \n\n"
"* %n\n"
)
"Default template used to create org-wiki pages/files.
- %n - is replaced by the page name.
- %d - is replaced by current date in the format year-month-day."
:type 'string
:group 'org-wiki
)
;; ********************** I N T E R N A L - F U N C T I O N S ************************* ;;
;;
;; @SECTION: Internal functionsq
;;
(defun org-wiki--concat-path (base relpath)
"Concat directory path (BASE) and a relative path (RELPATH)."
(concat (file-name-as-directory base) relpath))
;; Initialize org-wiki-location variable if not set yet.
;;
(defun org-wiki--start-location ()
(if (not org-wiki-location)
(setq org-wiki-location (car org-wiki-location-list))))
(defun org-wiki--get-buffers ()
"Return all org-wiki page buffers (.org) files in `org-wiki-location`."
(org-wiki--start-location)
(cl-remove-if-not (lambda (p)
(let* ((fp (buffer-file-name p))
(fpath (if fp (expand-file-name fp) nil))
)
;; path test if file exists (if fpath not nil)
(and fpath
;; test if buffer file is in wiki location
(string-prefix-p (expand-file-name org-wiki-location) fpath)
;; test if buffer file has extension .org
(string-suffix-p ".org" fpath)
)))
(buffer-list)))
(defun org-wiki--normalize-path (path)
"Replace double slashes for a single slash and remove slash at the end of a PATH."
(replace-regexp-in-string
"//"
"/"
(replace-regexp-in-string "/$" "" (expand-file-name path))))
(defun org-wiki--path-equal (p1 p2)
"Test if paths P1 and P2 are equal."
(equal (org-wiki--normalize-path p1) (org-wiki--normalize-path p2)))
(defun org-wiki--file->page (filename)
"Get a wiki page name from a FILENAME.
Example:
ELISP> (file->org-wiki--page \"Spanish.org\")
\"Spanish\""
(file-name-base filename))
(defun org-wiki--replace-extension (filename extension)
"Replace a FILENAME extension by an new EXTENSION.
Example:
ELISP> (org-wiki/replace-extension \"file.org\" \"html\" )
\"file.html\""
(concat (car (split-string filename "\\."))
"."
extension
))
(defun org-wiki--page->file (pagename)
"Get the corresponding wiki file (*.org) to the wiki PAGENAME.
Example:
ELISP> (org-wiki--page->file \"Linux\")
\"~/org/wiki/Linux.org\""
(concat (file-name-as-directory org-wiki-location)
pagename
".org"
))
(defun org-wiki--current-page ()
"Get current org-wiki page's name bound to current buffer."
(org-wiki--file->page (buffer-file-name)))
(defun org-wiki--current-page-asset-dir ()
"Get current org-wiki page's asset directory"
(interactive)
(concat (file-name-as-directory org-wiki-location)
(file-name-base (buffer-file-name))))
(defun org-wiki--current-page-asset-file (filename)
"Get current page's asset file path given its name.
Example: If the current page is 'Smalltalk programming'
ELISP> (org-wiki--current-page-asset-file \"manual.pdf\")
\"Smalltalk programming/manual.pdf\"
ELISP>"
(concat (file-name-as-directory (file-name-base (buffer-file-name)))
filename))
(defun org-wiki--buffer-file-in-wiki-p ()
"Return true if current buffer file name is inside wiki directory."
(file-exists-p
(org-wiki--concat-path
org-wiki-location
(file-name-nondirectory (buffer-file-name)))))
(defun org-wiki--list-pages ()
"Return a list containing all pages files *.org."
(directory-files org-wiki-location))
(defun org-wiki--page->html-file (pagename)
"Convert a wiki PAGENAME to html file name."
(concat (file-name-as-directory (expand-file-name org-wiki-location))
pagename
".html"
))
(defun org-wiki--page-files (&optional abspath)
"Return a list containing all files in the wiki directory.
\(org-wiki--page-files &optional ABSPATH)
if abspath is null returns relative path, otherwise returns the absolute path.
Example:
ELISP> (remove-if-not #'file->org-wiki/page (org-wiki/page-files))
(\"Abreviations_Slangs.wiki.org\" \"Android.wiki.org\" \"Bash_Script.wiki.org\")"
(org-wiki--start-location)
(cl-remove-if-not
(lambda (s)
(let ((b (file-name-base s)))
(not (or
(string-prefix-p ".#" b)
(string-suffix-p "~" b )
(string-prefix-p "#" b)
(string-suffix-p "#" b)
))))
(directory-files org-wiki-location abspath "\\.org$")))
(defun org-wiki--page-list ()
"Return a list containing all wiki pages.
Example: '(\"Linux\" \"BSD\" \"Bash\" \"Binary_Files\")"
(mapcar #'org-wiki--file->page (org-wiki--page-files)))
;; @REVIEW: Function for future use.
;;
;; (defun org-wiki--get-page (wikipage)
;; (org-wiki--concat-path org-wiki-location
;; (replace-regexp-in-string "\s" "_"
;; (replace-regexp-in-string "%20" "_"
;; (concat wikipage ".org")))))
(defun org-wiki--assets-get-dir (pagename)
"Get path to asset directory of given PAGENAME."
(org-wiki--concat-path org-wiki-location pagename))
(defun org-wiki--assets-make-dir (pagename)
"Create the asset directory of a wiki page (PAGENAME) if it doesn't exist.
Example: (org-wiki--assets-make-dir \"Bash\")
It will crate the directory ~/wiki-location/Bash/
corresponding to the file ~/wiki-location/Bash.org
if it doesn't exist yet."
(let ((assets-dir (org-wiki--assets-get-dir pagename)))
(if (not (file-exists-p assets-dir))
(make-directory assets-dir t))))
(defun org-wiki--assets-buffer-make-dir ()
"Create asset directory of current buffer page if it doesn't exit."
(if (org-wiki--buffer-file-in-wiki-p)
(progn
(org-wiki--assets-make-dir
(file-name-base (buffer-file-name))))
(message "Error: Not in a wiki page.")))
(defun org-wiki--is-buffer-in (b)
"Check if buffer is an org-wiki buffer.
It returns true (non nil) if buffer directory is a subdirectory of
org-wiki-location."
(string-prefix-p
(expand-file-name org-wiki-location)
(expand-file-name (with-current-buffer b
default-directory))))
;;=============== Org-mode custom protocol ===============;;
;;
;; @SECTION: Protocol
(defun org-wiki--org-link (path desc backend)
"Creates an html org-wiki pages when exporting to html.
Example: The hyperlink [[wiki:Linux][Dealing with Linux]]
will be exported to <a href='Linux.html'>Dealing with Linux</a>"
(cl-case backend
(html (format
"<a href='%s.html'>%s</a>"
path
(or desc path)))))
(defun org-wiki--make-link (pagename)
"Return a string containing a wiki link [[wiki:PAGENAME][PAGENAME]].
Example: if PAGENAME is Linux it will return [[wiki:Linux][Linux]]"
(format "[[wiki:%s][%s]]" pagename pagename))
(defun org-wiki--open-page (pagename)
"Open or create new a org-wiki page (PAGENAME) by name.
Example: (org-wiki--open-page \"Linux\")
Will open the the wiki file Linux.org in
`org-wiki-location`"
(let ((org-wiki-file (org-wiki--page->file pagename)))
(if (not (file-exists-p org-wiki-file))
;; Action executed if file doesn't exist.
(progn (find-file org-wiki-file)
;; Insert header at top of page
(org-wiki-header)
;; Save current page buffer
(save-buffer)
;; Create assets directory
(org-wiki--assets-make-dir pagename))
;; Action executed if file exists.
(if org-wiki-default-read-only
;; open file in read-only mode.
(progn (find-file org-wiki-file)
(read-only-mode 1))
;; open file in writable mode.
(find-file org-wiki-file))
)))
(defun org-wiki--assets-get-file (pagename filename)
"Return a path to an asset file FILENAME in given PAGENAME."
(org-wiki--concat-path (org-wiki--assets-get-dir pagename) filename))
(defun org-wiki--assets-open-file-emacs (pagename filename)
"Open an asset file FILENAME of a PAGENAME with Emacs.
Example: (org-wiki--assets-open-file-emacs \"Python\" \"example1.py\")
It will open the file <wiki path>/Python/example1.py related to the page Python.org."
(find-file (org-wiki--assets-get-file pagename filename)))
(defun org-wiki-xdg-open (filename)
"Open a file FILENAME with default system application.
This function is operating system independent.
Running in Linux or BSD invokes the script xdg-open
Running in Windows invokes cmd.exe
Running in Mac OSX invokes open"
(cl-case system-type
;;; Linux
(gnu/linux (let ((process-connection-type nil))
(start-process
"proc"
nil
;; Command
"xdg-open" (expand-file-name filename))))
;;; Free BSD OS
(gnu/kfreebsd (let ((process-connection-type nil))
(start-process
"proc"
nil
;; Command
"xdg-open" (expand-file-name filename))))
;; Mac OSX - Tested. Ok.
(darwin (start-process
"proc"
nil
;; Command
"open" (concat (expand-file-name filename))))
;; Windows 7, 8, 10 - Kernel NT
(windows-nt (start-process
"proc"
nil
;; Command
"cmd" "/C" "start" "" (expand-file-name filename))
))) ;; End of org-wiki/xdg-open
(defun org-wiki--protocol-open-assets-with-sys (link)
"Org-mode protocol handler to open an asset with default system app.
Example: it will turn a hyperlink LINK of syntax Blueprint;box1.dwg that
points to the file <org wiki location>/Blueprint/box1.dwg."
(let* ((a (split-string link ";"))
(pagename (car a))
(filename (cadr a))
)
(org-wiki-xdg-open
(org-wiki--assets-get-file pagename filename))))
;; @DONE: Implement html exporting to org-wiki asset files
;;
(defun org-wiki--asset-link (path desc backend)
"Creates an html org-wiki pages html exporting."
(let* ((a (split-string path ";"))
(page (car a))
(asset (cadr a))
(file-path (concat page "/" asset))
)
(cl-case backend
(html (format
"<a href='%s'>%s</a>"
file-path
(or desc asset))))))
;;; Custom Protocols
(add-hook 'org-mode-hook
(lambda ()
;; Hyperlinks to other org-wiki pages
;;
;; wiki:<page-name> or [[wiki:<page-name>][<page-name>]]
(org-add-link-type "wiki"
#'org-wiki--open-page
#'org-wiki--org-link )
;; Hyperlinks to asset files that are opened with system
;; applications such as spreadsheets.
;;
;; wiki-asset-sys:<
(org-add-link-type "wiki-asset-sys"
#'org-wiki--protocol-open-assets-with-sys
#'org-wiki--asset-link)))
(defun org-wiki--helm-selection (callback)
"Open a helm menu to select the wiki page and invokes the CALLBACK function."
(helm :sources `((
(name . "Wiki Pages")
(candidates . ,(delete-dups (org-wiki--page-list)))
(action . ,callback)
))))
(defun org-wiki--asset-page-files (pagename)
"Get all asset files from a given PAGENAME."
(org-wiki--assets-make-dir pagename)
(directory-files (org-wiki--assets-get-dir pagename)))
(defun org-wiki--asset-helm-selection (callback)
"Higher order function to deal with page assets.
org-wiki-asset-helm-selection (CALLBACK)
This function opens a helm menu to select a wiki page and then
passes the result of selection to a callback function that takes
a asset file as argument.
Example: If the user selects the file freebsdref1.pdf it inserts the
file name at current point.
> (org-wiki--asset-helm-selection (lambda (file) (insert file)))
freebsdref1.pdf"
(helm :sources `((
(name . "Wiki Pages")
(candidates . ,(org-wiki--asset-page-files
(org-wiki--current-page)))
(action . (lambda (file)
(,callback (org-wiki--current-page-asset-file file))
))
))))
(defun org-wiki--asset-download-hof (callback)
"Higher order function to download a file.
Callback is a function with this signature:
(callback <pagename> <filename>)
How this function works:
1. Ask the user for the URL suggesting the URL extracted from the clipboard.
2. Ask the user for the file name to be downloaded suggesting the filename extracted from
the URL.
3. Calls the callback function passing the current page name and the file name.
If the URL is: http://www.myurl.com/Manual1.pdf, the current page is Unix and
the callback function is:
(lambda (p f) (insert (format \"%s/%s\" p f)))
if the user doesn't change the suggested file name It will insert at current
point: 'Unix/Manual.pdf'."
(let*
((pagename (file-name-base (buffer-file-name)))
;; Get the URL suggestion from clibpoard
(text (with-temp-buffer
(clipboard-yank)
(buffer-substring-no-properties (point-min)
(point-max))))
(url (read-string "Url: " text))
(default-directory (org-wiki--assets-get-dir pagename))
(output-file (read-string "File name: "
(car (last (split-string url "/"))))))
(org-wiki--assets-make-dir pagename)
(url-copy-file url output-file)
(funcall callback pagename output-file)))
;;************** U S E R - M-X - C O M M A N D S ********************* ;;;
;;
;; @SECTION: User commands
(defun org-wiki-help ()
"Show org-wiki commands."
(interactive)
(command-apropos "org-wiki-"))
(defun org-wiki-switch-root ()
"Switch org-wiki root directory"
(interactive)
(helm :sources
`((name . "Org-wiki root dir")
(candidates . ,(mapcar (lambda (p)
(cons (format "%s - %s" (file-name-nondirectory p) p) p))
(mapcar #'string-trim org-wiki-location-list)))
(action . (lambda (p)
;; If custom variable is set to true, then close all org-wiki pages of current
;; org-wiki root directory
(if org-wiki-close-root-switch (org-wiki-close))
;; set new org-wiki location
(setq org-wiki-location p)
;; Go to index page
(org-wiki-index)
;; Inform user about new directory
(message (format "Org-wiki root dir set to: %s" p))
)))))
(defun org-wiki-index ()
"Open the index page: <org-wiki-location>/index.org.
The file index.org is created if it doesn't exist."
(interactive)
(org-wiki--open-page org-wiki-index-file-basename))
(defun org-wiki-index-html ()
"Open the Wiki (Index) in the default web browser."
(interactive)
(browse-url (concat "file://"
(org-wiki--page->html-file
org-wiki-index-file-basename))))
(defun org-wiki-index-frame ()
"Open the index page in a new frame."
(interactive)
(with-selected-frame (make-frame)
(org-wiki-index)))
(defun org-wiki-dired-all ()
"Open the wiki directory in ‘dired-mode’ showing all files."
(interactive)
(dired org-wiki-location)
(dired-hide-details-mode))
(defun org-wiki-dired ()
"Open the wiki directory showing only the wiki pages."
(interactive)
(dired (org-wiki--concat-path org-wiki-location "*.org"))
(dired-hide-details-mode))
(defun org-wiki-asset-dired ()
"Open the asset directory of current wiki page."
(interactive)
(let ((pagename (file-name-base (buffer-file-name))))
(org-wiki--assets-make-dir pagename)
(dired (org-wiki--assets-get-dir pagename))))
(defun org-wiki-asset-insert ()
"Insert link wiki-asset-sys:<page>;<file> to an asset file of current page..
It inserts a link of type wiki-asset-sys:<Wiki-page>;<Asset-File>
Example: [[wiki-asset-sys:Linux;LinuxManual.pdf]]"
(interactive)
(org-wiki--asset-helm-selection
(lambda (file)
(insert (format "[[wiki-asset-sys:%s;%s][%s]]"
(file-name-base (org-wiki--current-page-asset-dir))
(file-name-nondirectory file)
(read-string "Description: " (file-name-nondirectory file))
)))))
(defun org-wiki-asset-insert-file ()
"Insert link file:<page>/<file> to asset file of current page at point.
Use this command to insert link to files that can be opened with
Emacs like source codes. It will insert a link like this
- [[file:Python/GpsScript.py][GpsScript.py]]."
(interactive)
(org-wiki--asset-helm-selection
(lambda (file)
(save-excursion
(insert (org-make-link-string
(concat "file:" file)
(file-name-nondirectory file)
))))))
(defun org-wiki-asset-insert-image ()
"Insert link file:<page>/<file> to images asset file at point.
This command is similar to org-wiki-asset-insert-file but it inserts a link
in this way: [[file:Linux/logo.png][file:Linux/logo.png/]]."
(interactive)
(org-wiki--asset-helm-selection
(lambda (file)
(save-excursion
(insert (org-make-link-string
(concat "file:" file)
(concat "file:" file)
))))))
(defun org-wiki-asset-insert-block ()
"Insert code block with contents of some asset file."
(interactive)
(org-wiki--asset-helm-selection
(lambda (file)
(save-excursion
(insert (concat " - File: " (org-make-link-string (concat "file:" file))))
(insert "\n\n")
(insert "#+BEGIN_SRC text\n")
(insert " ")
(insert (replace-regexp-in-string
"\n"
"\n "
(with-temp-buffer
(insert-file-contents file)
(buffer-substring-no-properties (point-min) (point-max)))))
(insert "\n#+END_SRC")
))))
(defun org-wiki-asset-find-file ()
"Open a menu to select an asset file of current page and open it with Emacs.
Note: see 'org-wiki-asset-find-sys'
Example: If the current page is 'Smalltalk programming' and the user select the
file 'extendingClasses-number1.gst' it will open the file below with Emacs.
- Smalltalk programming/'extendingClasses-number1.gst"
(interactive)
(org-wiki--asset-helm-selection #'find-file))
(defun org-wiki-asset-find-sys ()
"Open a menu to select an asset file of current page and open it with system's app.
Example: If the current page is 'Smalltalk programming' and the
user select the file 'numerical-methods-in-smalltalk.pdf' it will
be opened with the default system's application like Foxit PDF or
Okular reader."
(interactive)
(org-wiki--asset-helm-selection #'org-wiki-xdg-open))
(defun org-wiki-asset-create ()
"Prompts the user for a file name that doesn't exist yet and insert it at point.
Unlike the commands `org-wiki-asset-insert` or ` org-wiki-asset-insert-file` this command
asks the user for a file that doesn't exist yet and inserts a hyperlink to it at point.
It is useful to add links to scripts that will be stored in the
page directory.
Example: If the user enter this command and is in the page Linux
and enters scriptDemoQT.py it will insert a link at point like
this file:Linux/scriptDemoQT.py .
- Page: <org-wiki-location>/Linux.org
- Directory: <org-wiki-location>/Linux/"
(interactive)
(let ((filename (read-string "File: ")))
(save-excursion
(insert (org-make-link-string
(concat "file:"
(org-wiki--current-page-asset-file filename))
filename
)))))
(defun org-wiki-asset-download-insert1 ()
"Download a file from a URL in the clibpoard and inserts a link wiki-asset-sys:.
Note: This function is synchronous and blocks Emacs. If Emacs is stuck
type C-g to cancel the download."
(interactive)
(org-wiki--asset-download-hof
(lambda (pagename output-file)
(save-excursion (insert (format "[[wiki-asset-sys:%s;%s][%s]]"
pagename output-file output-file))))))
(defun org-wiki-asset-download-insert2 ()
"Download a file from a URL in the clibpoard and inserts a link file:<page>/<asset-file>.
Note: This function is synchronous and blocks Emacs. If Emacs gets frozen type C-g
to cancel the download."
(interactive)
(org-wiki--asset-download-hof
(lambda (pagename output-file)
(save-excursion (insert (format "file:%s/%s" pagename output-file ))))))
(defun org-wiki-helm ()
"Browser the wiki files using helm."
(interactive)
(org-wiki--helm-selection #'org-wiki--open-page))
(defun org-wiki-helm-read-only ()
"Open wiki page in read-only mode."
(interactive)
(org-wiki--helm-selection (lambda (pagename)
(find-file-read-only
(org-wiki--page->file pagename)
))))
(defun org-wiki-helm-frame ()
"Browser the wiki files using helm and opens it in a new frame."
(interactive)
(org-wiki--helm-selection (lambda (act)
(with-selected-frame (make-frame)
(set-frame-name (concat "Org-wiki: " act))
(org-wiki--open-page act)
))))
(defun org-wiki-switch ()
"Switch between org-wiki page buffers."
(interactive)
(helm :sources `((
(name . "Wiki Pages")
(candidates . ,(mapcar (lambda (b)
(cons (org-wiki--file->page (buffer-file-name b))
b
))
(org-wiki--get-buffers)))
(action . switch-to-buffer)
))))
;; @TODO: Implement org-wiki/helm-html
;;
(defun org-wiki-helm-html ()
"Browser the wiki files using helm."
(interactive)
(helm :sources `((
(name . "Wiki Pages")
(candidates . ,(delete-dups (org-wiki--page-list)))
(action . org-wiki--open-page)
))))
(defun org-wiki-close ()
"Close all opened wiki pages buffer and save them."
(interactive)
(mapc (lambda (b)
(with-current-buffer b
(when (org-wiki--is-buffer-in b)
;; save the buffer if it is bound to a file
;; and it is not read-only
(when (and (buffer-file-name b)
(not buffer-read-only))
(save-buffer))
(kill-this-buffer))))
(buffer-list))
(message "All wiki files closed. Ok."))
(defun org-wiki-close-image ()
"Close all image/picture buffers which files are in org-wiki directory."
(interactive)
(mapc (lambda (b)
(with-current-buffer b
(when (and (org-wiki--is-buffer-in b)
(equal major-mode 'image-mode))
(kill-this-buffer))))
(buffer-list))
(message "All wiki images closed. Ok."))
(defun org-wiki-insert-link ()
"Insert a Wiki link at point for a existing page."
(interactive)
(org-wiki--helm-selection
(lambda (page) (insert (org-wiki--make-link page)))))
(defun org-wiki-insert-new ()
"Create a new org-wiki and insert a link to it at point."
(interactive)
(let ((page-name (read-string "Page: ")))
(save-excursion (insert (org-make-link-string (concat "wiki:" page-name)
page-name
)))))
(defun org-wiki-new ()
"Create a new wiki page and open it without inserting a link."
(interactive)
(org-wiki--open-page (read-string "Page Name: ")))
(defun org-wiki-html-page ()
"Open the current wiki page in the browser. It is created if it doesn't exist yet."
(interactive)
(let ((html-file (org-wiki--replace-extension (buffer-file-name) "html")))
(if (not (file-exists-p html-file))
(org-html-export-to-html))
(browse-url html-file)))
(defun org-wiki-html-page2 ()
"Exports the current wiki page to html and opens it in the browser."
(interactive)
(org-html-export-to-html)
(browse-url (org-wiki--replace-extension (buffer-file-name) "html")))
(defun org-wiki-search ()
"Search all wiki pages that contains a pattern (regexp or name)."
(interactive)
(rgrep (read-string "org-wiki - Search for: ")
"*.org"
org-wiki-location
nil))
(defun org-wiki-open ()
"Opens the wiki repository with system's default file manager."
(interactive)
(org-wiki-xdg-open org-wiki-location))
(defun org-wiki-asset-open ()
"Open asset directory of current page with system's default file manager."
(interactive)
(org-wiki--assets-buffer-make-dir)
(org-wiki-xdg-open (file-name-base (buffer-file-name))))
(defun org-wiki-assets-helm ()
"Open the assets directory of a wiki page."
(interactive)
(org-wiki--helm-selection
(lambda (page)
(org-wiki--assets-make-dir page)
(dired (org-wiki--assets-get-dir page)))))
(defun org-wiki-make-org-publish-plist (org-exporter)
"Prepare plist for use with `org-publish'."
(let ((plist-base
`("html"
:base-directory ,org-wiki-location
:base-extension "org"
:publishing-directory ,org-wiki-location
:publishing-function ,org-exporter)))
(setcdr plist-base
(org-combine-plists (cdr plist-base) org-wiki-publish-plist))
plist-base))
(defun org-wiki-export-with (org-exporter)
"Export all pages to a given format. See full doc.
ORG-EXPORTER is a function that exports an org-mode page to a specific format like html.
It can be for instance:
- org-html-publish-to-thml
- org-latex-publish-to-pdf
- org-latex-publish-to-latex
WARN: This is a synchronous function and can freeze Emacs. Emacs will freeze while
the exporting doesn't finish. Type C-g to abort the execution."
(interactive)
(let ((org-html-htmlize-output-type 'css)
(org-html-htmlize-font-prefix "org-")
(pub-plist (org-wiki-make-org-publish-plist org-exporter))
)
(org-publish pub-plist t)))
(defun org-wiki-export-html-sync ()
"Export all pages to html in synchronous mode."
(interactive)
(let ((org-html-htmlize-output-type 'css)
(org-html-htmlize-font-prefix "org-")
(pub-plist (org-wiki-make-org-publish-plist 'org-html-publish-to-html))
)
(org-publish pub-plist t)))
(defun org-wiki-export-html ()
"Export all pages to html.
Note: This function doesn't freeze Emacs since it starts another Emacs process."
(interactive)
(compile (mapconcat 'identity
`(,org-wiki-emacs-path
"--batch"
"-l" ,org-wiki-user-init-file
"-f" "org-wiki-export-html-sync"
"--kill"
)
" "
)))
(defun org-wiki-make-menu ()
"Optional command to build an utility menu."
(interactive)
(easy-menu-define org-wik-menu global-map "Org-wiki"
`("org-wiki"
("Main"
["Go to Index page \nM-x org-wiki-index" (org-wiki-index)]
["---" nil]
["Browsing" nil]
["Browse page \nM-x org-wiki-helm" (org-wiki-helm)]
["Browse page in other frame \nM-x org-wiki-helm-frame" (org-wiki-helm-frame)]
["Browse pages in read-only mode \nM-x org-wiki-helm-read-only" (org-wiki-helm-read-only)]
["---" nil]
["Wiki Directory" nil]
["Open org-wiki directory \nM-x org-wiki-dired" (org-wiki-dired)]
["Open org-wiki directory with system's file manager.\nM-x org-wiki-open" (org-wiki-open)]
["Close all pages \nM-x org-wiki-close" (org-wiki-close)]
["---" nil]
["Html export" nil]
["Open index page (html) in the browser \nM-x org-wiki-index-html" (org-wiki-index-html)]
["Export all pages to html \nM-x org-wiki-export-html" (org-wiki-export-html)]
["Help - Show all org-wiki commands \nM-x org-wiki-help" (org-wiki-help)]
)
["---" nil]
("Page Commands"
["Browse current page asset directory.\nM-x org-wiki-asset-dired"
(org-wiki-asset-dired)]
["Browse current page asset directory with system's file manager.\nM-x org-wiki-asset-open"
(org-wiki-asset-open)]
["Insert a link to a wiki page \nM-x org-wiki-insert" (org-wiki-insert)]
["Insert a link of type wiki-asset-sys at point.\nM-x org-wiki-asset-insert"
(org-wiki-asset-insert)]
["Insert a link of type file:<page>/<asset> at point.\nM-x org-wiki-asset-insert-file"
(org-wiki-asset-insert-file)
]
["Download an asset file and insert a wiki-asset-sys link at point.\nM-x org-wiki-asset-download-insert1"
(org-wiki-asset-download-insert1)
]