Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rewrite of clip, do_clip and clip_again - more robust, better formatt…

…ing of output.

Niggling bug where once in a blue moon the linkback to a non-wiki page will be interpreted as a wiki page. Can't reproduce,
so I can't fix it.
  • Loading branch information...
commit 861935bf8e18642a27edd8f33b76e08bc4313d83 1 parent 2c1bbaa
Stian Håklev authored April 11, 2012

Showing 1 changed file with 42 additions and 22 deletions. Show diff stats Hide diff stats

  1. 64  dokuwiki.rb
64  dokuwiki.rb
@@ -202,10 +202,9 @@ def add_to_rss
202 202
 # pops up dialogue box, asking where to send text, takes selected text (or just link, if desired) and inserts at the bottom
203 203
 # of the selected page, with a context-relevant reference to original source
204 204
 def do_clip(pagename, titletxt, onlytext = false)
205  
-  pagepath = ("#{Wiki_path}/data/pages" + "/" + clean_pagename(pagename) + ".txt").gsub(":","/")
  205
+  pagepath = ("#{Wiki_path}/data/pages/#{clean_pagename(pagename)}.txt").gsub(":","/")
206 206
 
207 207
   curpage = cururl.split("/").last
208  
-
209 208
   sel = has_selection
210 209
 
211 210
   # format properly if citation
@@ -215,58 +214,79 @@ def do_clip(pagename, titletxt, onlytext = false)
215 214
     elsif cururl.index("localhost/wiki")
216 215
       curpage = "[[:#{capitalize_word(curpage.gsub("_", " "))}]]"
217 216
     else
218  
-      title = (titletxt == "" ? title : titletxt)
  217
+      title = (titletxt ? titletxt : curtitle)
219 218
       curpage ="[[#{cururl}|#{title}]]"
220 219
     end
221 220
   else
222 221
     curpage = ''
223 222
   end
224 223
 
225  
-  insert = (sel ? sel : "  *" )
  224
+  insert = (sel ? "#{sel} " : "  * " )   # any text, or just a link (bullet list)
  225
+  insert.gsubs!( {:all_with=> "\n\n"}, "\n", "\n\n\n" )
226 226
 
227 227
   if File.exists?(pagepath)
228  
-    f = File.read(pagepath)
  228
+    prevcont = File.read(pagepath)
  229
+
  230
+    haslinks = prevcont.match(/\-\-\-(\n  \*[^\n]+?)+?\Z/m)   # a "---"" followed by only lines starting with "  * "
  231
+
  232
+    # bullet lists need an extra blank line after them before the "----"
  233
+    if sel
  234
+      divider = (haslinks ? "\n\n----\n" : "\n----\n")
  235
+    else
  236
+      divider = (haslinks ? "\n" : "\n----\n")
  237
+    end
  238
+
229 239
     growltext = "Selected text added to #{pagename}"
230 240
   else
231  
-    f = "h1. "+ capitalize_word(pagename) + "\n\n"
  241
+    prevcont = "h1. #{capitalize_word(pagename)}\n\n"
232 242
     growltext = "Selected text added to newly created #{pagename}"
233 243
   end
234  
-  filetext = f + "\n----\n" + insert.gsubs({:all_with=> "\n\n"}, "\n", "\n\n\n") + " " + curpage
235  
-
  244
+  filetext = [prevcont, divider, insert, curpage].join
236 245
   dwpage(pagename, filetext)
  246
+
237 247
   growl("Text added", growltext)
238 248
 end
239 249
 
240 250
 def clip
241 251
   require 'pashua'
242  
-  title = curtitle
  252
+  title = curtitle.strip
243 253
 
244 254
   # asks for a page name, and appends selected text on current page to that wiki page, with proper citation
245  
-  pagetmp = wikipage_selector("Which wikipage do you want to add text to?",true, "
246  
-  ob.type = checkbox
247  
-  ob.label = do not include citation information, only insert pure text
248  
-  fb.type = textbox
249  
-  fb.default = #{title.strip}
250  
-  fb.label = Link title\n"
251  
-  )
  255
+  gui = "
  256
+    ob.type = checkbox
  257
+    ob.label = do not include citation information, only insert pure text
  258
+    fb.type = textbox
  259
+    fb.default = #{title}
  260
+    fb.label = Link title\n"
252 261
 
  262
+  gui << "ob.disabled = 1\n" unless has_selection # no point in only inserting text, if no text selected
  263
+
  264
+  # get last page inserted to as default, if exists
  265
+  lastclip = try { File.read("/tmp/dokuwiki-clip.tmp").split("\n") }
  266
+  gui << "cb.default = #{lastclip[0]}\n" if lastclip
  267
+
  268
+  pagetmp = wikipage_selector("Which wikipage do you want to add text to?", true, gui)
253 269
   exit if pagetmp["cancel"] == 1
  270
+
254 271
   onlytext = pagetmp['ob'] == "1" ? true : false
255 272
   pagename = pagetmp['cb'].strip
256 273
   pashua_title = pagetmp['fb'].strip
257  
-  filetitle = (title.strip == pashua_title) ? nil : pashua_title
  274
+  filetitle = (title.strip == pashua_title.strip) ? nil : pashua_title
  275
+
  276
+  # store for clip_again
258 277
   File.write("/tmp/dokuwiki-clip.tmp","#{pagename}\n#{cururl}\n#{filetitle}\n#{onlytext.to_s}")
259  
-  do_clip(pagename, title, onlytext)
  278
+
  279
+  do_clip(pagename, filetitle, onlytext)
260 280
 end
261 281
 
262 282
 # uses info stored in temp file to do a clipping from the same page, to the same page
263 283
 def clip_again
264 284
   a = File.read("/tmp/dokuwiki-clip.tmp")
265 285
   page, url, title, onlytext_s = a.split("\n")
266  
-  onlytext = (onlytext_s == 'true') ? true : false
267  
-  if title.strip == "" || url != cururl
268  
-    title = curtitle
269  
-  end
  286
+  onlytext = (onlytext_s == 'true' && has_selection)
  287
+
  288
+  title = curtitle if (title.strip == "") || (url != cururl)
  289
+
270 290
   do_clip(page, title, onlytext)
271 291
 end
272 292
 

0 notes on commit 861935b

Please sign in to comment.
Something went wrong with that request. Please try again.