1
2 Author:peter@peterodding.com
3 Last Change:
4 URL:http://peterodding.com/code/vim/publish/
5 License:
6 Version:
7
8
9 GetLatestVimScripts::AutoInstall
10
11 &compatible
12 if &cp || exists('g:loaded_publish')
13 finish
14 endif
15
16 if !exists('g:publish_omit_dothtml')
17 let g:publish_omit_dothtml = 0
18 endif
19
20 if !exists('g:publish_plaintext')
21 let g:publish_plaintext = 0
22 endif
23
24 function! Publish(source, target, files) abort
25 let start = xolox#timer#start()
26 call xolox#message("Preparing to publish file%s ..", len(a:files) == 1 ? '' : 's')
27 let s:files_to_publish = publish#resolve_files(a:source, a:files)
28 call publish#update_tags(values(s:files_to_publish))
29 let s:tags_to_publish = publish#find_tags(s:files_to_publish)
30 if s:tags_to_publish != {}
31 let tags_to_links_command = publish#create_subst_cmd(s:tags_to_publish)
32 endif
33 let rsync_target = publish#rsync_check(a:target)
34 if rsync_target != ''
35 let rsync_dir = xolox#path#tempdir()
36 endif
37 let target_dir = rsync_target != '' ? rsync_dir : a:target
38 call publish#prep_env(1)
39 for pathname in a:files
40 let source_path = xolox#path#merge(a:source, pathname)
41 let suffix = g:publish_omit_dothtml ? '' : '.html'
42 let target_path = xolox#path#merge(target_dir, pathname . suffix)
43 call xolox#message("Publishing %s", string(pathname))
44 if !publish#create_dirs(target_path)
45 return
46 endif
47
48 s:ConvertTagToLink
49 let given_source = s:FindOriginalPath(source_path)
50 let s:current_source_directory = fnamemodify(given_source, ':h')
51 silent execute 'edit!' fnameescape(source_path)
52 if g:publish_plaintext
53 let plaintext_path = xolox#path#merge(target_dir, pathname . '.txt')
54 silent execute 'write!' fnameescape(plaintext_path)
55 endif
56
57 if exists('g:loaded_easytags')
58 HighlightTags
59 endif
60 let highlight_start = xolox#timer#start()
61 call publish#munge_syntax_items()
62 runtime syntax/2html.vim
63 let msg = "publish.vim: The 2html.vim script took %s to highlight %s."
64 call xolox#timer#stop(msg, highlight_start, pathname)
65 if exists('tags_to_links_command')
66 let tags_to_links_start = xolox#timer#start()
67 silent execute tags_to_links_command
68 let msg = "publish.vim: Finished converting tags in %s to links in %s."
69 call xolox#timer#stop(msg, pathname, tags_to_links_start)
70 endif
71 call publish#customize_html(pathname)
72 silent execute 'write!' fnameescape(target_path)
73 bwipeout!
74 endfor
75 unlet s:files_to_publish s:tags_to_publish
76 if rsync_target != ''
77 call publish#run_rsync(rsync_target, rsync_dir)
78 endif
79 let msg = "publish.vim: Published %i file%s to %s."
80 call xolox#message(msg, len(a:files), len(a:files) == 1 ? '' : 's', a:target)
81 call xolox#timer#stop("Finished publishing files in %s.", start)
82 call publish#prep_env(0)
83 endfunction
84
85 function! s:FindOriginalPath(pathname)
86 let key = xolox#path#absolute(a:pathname)
87 return get(s:files_to_publish, key, '')
88 endfunction
89
90 function! s:ConvertTagToLink(name)
91
92
93
94 try
95
96 let text = substitute(a:name, '<[^>]\+>', '', 'g')
97 if has_key(s:tags_to_publish, text)
98 let entry = s:tags_to_publish[text]
99 else
100 let text = substitute(text, '^\(s:\|<[Ss][Ii][Dd]>\)', '', 'g')
101 let entry = s:tags_to_publish[text]
102 endif
103
104 let pathname = s:FindOriginalPath(entry.filename)
105
106 TODO
107 let relative = xolox#path#relative(pathname, s:current_source_directory)
108 let suffix = g:publish_omit_dothtml ? '' : '.html'
109 let href = publish#html_encode(relative . suffix . '#l' . entry.lnum)
110 return '<a href="' . href . '">' . a:name . '</a>'
111 catch
112 return a:name
113 endtry
114 endfunction
115
116 let g:loaded_publish = 1
117
118