Created
January 25, 2012 15:05
-
-
Save ttscoff/1676667 to your computer and use it in GitHub Desktop.
Revisions
-
ttscoff revised this gist
Jan 31, 2012 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,6 +44,7 @@ end opts.on( '-v', 'Display version number and exit') do puts "ScrivWatcher v#{$version}" exit end opts.on( '-h', '--help', 'Display this screen' ) do puts opts -
ttscoff revised this gist
Jan 31, 2012 . 1 changed file with 121 additions and 54 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ #!/usr/bin/ruby # scrivwatch.rb by Brett Terpstra, 2011 # Modifications to merge into one file for Markdown viewing in Marked by BooneJS, 2012 # Modified to use REXML and add titles by Brett Terpstra, 2012 <https://gist.github.com/1676667> @@ -9,13 +9,53 @@ require 'fileutils' require 'rexml/document' require 'optparse' $version = "1.5" $out = STDOUT $progressbar = STDERR $options = {} optparse = OptionParser.new do|opts| opts.banner = "ScrivWatcher version #{$version}\nUsage: scrivwatcher.rb [-dqt] [--cache-dir PATH] /path/to/document.scriv" $options[:debug] = false opts.on( '-d','--debug', 'Debug mode' ) do $options[:debug] = true end $options[:progress] = false opts.on('-p','--progress','Send progress bar commands for Platypus') do $options[:progress] = true end $options[:quiet] = false opts.on( '-q','--quiet', 'Run quietly (no progress bar/messages)' ) do $options[:quiet] = true unless $options[:progress] end $options[:titles] = false opts.on( '-t', '--titles', 'Generate Markdown headers from Scrivener page titles' ) do $options[:titles] = true end $options[:cachedir] = File.expand_path("~/ScrivWatcher") opts.on( '-c','--cache-dir PATH', 'Define the directory for cache files (default "~/ScrivWatcher"') do|cachedir| $options[:cachedir] = File.expand_path(cachedir).gsub(/\/$/,'') end $options[:openscriv] = false opts.on( '--open-scrivener', 'When loading a file in Marked, open it in Scrivener automatically') do $options[:openscriv] = true end opts.on( '-v', 'Display version number and exit') do puts "ScrivWatcher v#{$version}" end opts.on( '-h', '--help', 'Display this screen' ) do puts opts exit end end optparse.parse! def check_running() newpid = '' newpid = %x{ps Ao pid,comm|grep "Marked.app"|grep -v grep|awk '{print $1}'} return newpid.empty? ? false : true end @@ -56,43 +96,54 @@ def get_rtf_file_array(scrivpath) return files end def usage puts "Usage: scrivwatcher.rb /path/to/document.scriv" puts "Use `scrivwatcher -h` for help." exit end trap("SIGINT") { exit } unless $options[:quiet] || $options[:debug] if (ARGV[0] !~ /\.scriv\/?$/) usage end end if $options[:debug] && ARGV.length == 0 path = File.expand_path("~/Dropbox/ScrivTest.scriv") else if ARGV[0] =~ /\.scriv\/?$/ path = File.expand_path(ARGV[0].gsub(/\/$/,'')) else usage end end sw_name = File.basename(path,".scriv") $out.printf("Watching %s:\n",sw_name) unless $options[:quiet] sw_target = File.expand_path($options[:cachedir]) Dir.mkdir(sw_target) unless File.exists?(sw_target) sw_cache_dir = File.expand_path("#{$options[:cachedir]}/cache") Dir.mkdir(sw_cache_dir) unless File.exists?(sw_cache_dir) sw_cache = File.expand_path("#{sw_cache_dir}/#{sw_name}") # Clear cache if File.exists?(sw_cache) File.delete(*Dir["#{sw_cache}/*"]) if Dir.glob("#{sw_cache}/*").length > 0 Dir.rmdir(sw_cache) end Dir.mkdir(sw_cache) sw_note = "#{sw_target}/ScrivWatcher - #{sw_name}.md" File.delete(sw_note) if File.exists?(sw_note) FileUtils.touch(sw_note) %x{open -a Scrivener "#{path}"} if $options[:openscriv] %x{open -a /Applications/Marked.app "#{sw_note}"} first = true files = [] @@ -111,7 +162,7 @@ def get_rtf_file_array(scrivpath) diff_xml_time = new_xml_time - xml_time unless diff_xml_time == 0 && first == false files = get_rtf_file_array(path) end # track any file changes in folder @@ -121,48 +172,64 @@ def get_rtf_file_array(scrivpath) arr = [0,10,20,30,40,50,60,70,80,90,100] unless first == false && diff_hash.empty? && diff_xml_time == 0 # if changes were found in the timestamps $out.print("change detected\n") unless first || $options[:quiet] hash = new_hash xml_time = new_xml_time cachefiles = [] total = files.length current = 0 files.each{ |f| current += 1 if f['path'] cachefile = sw_cache + "/" + File.basename(f['path'],'.rtf') + ".md" if !File.exists?(cachefile) || (File.stat(f['path']).mtime.to_i > File.stat(cachefile).mtime.to_i) # $stdout.puts "Caching #{f['path']}" if $options[:debug] note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" if $options[:titles] && !f['depth'].nil? f['depth'].times { leader += "#" } leader = "#{leader} #{f['title']}\n\n" end notetext = leader + note + "\n\n" File.open(cachefile,"w"){|cf| cf.puts notetext } end cachefiles.push(cachefile) unless $options[:quiet] #progress bar percent = (current * 100 / total).ceil progress = arr.select{|item| item <= percent }.max cache_message = first ? "Building cache:" : "Updating cache:" if $options[:progress] $progressbar.printf("%s\n",cache_message) $progressbar.printf("PROGRESS:%d\n",progress) else $out.printf("%s [",cache_message) $out.print("=="*(progress/10)) $out.print(" "*(10-(progress/10))) unless progress == 100 $out.print("]") $out.printf(" %d%%\r",percent) $out.flush end end end } $out.print("\n") unless $options[:quiet] first = false # write the result to the preview file concat_note = "Concatenating #{cachefiles.length} sections to #{sw_note}..." if $options[:progress] $progressbar.printf("%s\n",concat_note) else $out.print(concat_note) unless $options[:quiet] end File.open(sw_note,"w"){|f| f.puts cachefiles.map{|s| IO.read(s)} } if $options[:progress] $progressbar.printf("PROGRESS:0\n") $progressbar.printf("Watching %s\n",sw_name) else $out.puts("Done.\nWatching...") unless $options[:quiet] end end sleep 1 -
ttscoff revised this gist
Jan 28, 2012 . 1 changed file with 34 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,9 +9,10 @@ require 'fileutils' require 'rexml/document' $debug = false $droplet = ENV['SW_DROPLET'] $titles_as_headers = false $out = STDOUT def check_running() newpid = %x{ps ax|grep Marked|grep -v grep|awk '{print $1}'} @@ -52,32 +53,28 @@ def get_rtf_file_array(scrivpath) end end return files end trap("SIGINT") { exit } unless $droplet || $debug if (ARGV.length != 1 || ARGV[0] !~ /\.scriv\/?$/) puts "Usage: scrivwatch.rb /path/to/document.scriv" exit end end if $debug && ARGV.length == 0 path = File.expand_path("~/Dropbox/ScrivTest.scriv") else path = File.expand_path(ARGV[0].gsub(/\/$/,'')) end sw_name = path.gsub(/.*?\/([^\/]+)\.scriv$/,"\\1") $out.printf("Watching %s:\n",sw_name) unless $droplet sw_target = File.expand_path("~/ScrivWatcher") Dir.mkdir(sw_target) unless File.exists?(sw_target) @@ -121,33 +118,51 @@ def concat(cachefiles,sw_note) new_hash = files.collect {|f| [ f['path'], File.stat(f['path']).mtime.to_i ] if f['path'] } # create a hash of timestamps hash ||= new_hash diff_hash = new_hash - hash # check for changes arr = [0,10,20,30,40,50,60,70,80,90,100] unless first == false && diff_hash.empty? && diff_xml_time == 0 # if changes were found in the timestamps $out.print("change detected\n") unless first || $droplet hash = new_hash xml_time = new_xml_time cachefiles = [] total = files.length current = 0 files.each{ |f| current += 1 if f['path'] cachefile = sw_cache + "/" + File.basename(f['path'],'.rtf') + ".md" if !File.exists?(cachefile) || (File.stat(f['path']).mtime.to_i > File.stat(cachefile).mtime.to_i) # $stdout.puts "Caching #{f['path']}" if $debug note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" if $titles_as_headers && !f['depth'].nil? f['depth'].times { leader += "#" } leader = "#{leader} #{f['title']}\n\n" end notetext = leader + note + "\n\n" File.open(cachefile,"w"){|cf| cf.puts notetext } end cachefiles.push(cachefile) unless $droplet #progress bar percent = (current * 100 / total).ceil progress = arr.select{|item| item <= percent }.max cache_message = first ? "Building cache:" : "Updating cache:" $out.print("#{cache_message} [") $out.print("=="*(progress/10)) $out.print(" "*(10-(progress/10))) unless progress == 100 $out.print("]") $out.printf(" %d%%\r",percent) $out.flush end end } $out.print("\n") unless $droplet first = false # write the result to the preview file $out.print("Concatenating #{cachefiles.length} sections to #{sw_note}...") unless $droplet File.open(sw_note,"w"){|f| f.puts cachefiles.map{|s| IO.read(s)} } $out.puts("Done.\nWatching...") unless $droplet end sleep 1 -
ttscoff revised this gist
Jan 28, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ require 'fileutils' require 'rexml/document' debug = false droplet = false titles_as_headers = false def check_running() -
ttscoff revised this gist
Jan 28, 2012 . 1 changed file with 54 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,7 @@ require 'rexml/document' debug = false droplet = true titles_as_headers = false def check_running() newpid = %x{ps ax|grep Marked|grep -v grep|awk '{print $1}'} @@ -20,8 +21,8 @@ def check_running() def get_children(ele,path,files,depth) ele.elements.each('*/BinderItem') do |child| # Ignore docs set to not include in compile includetag = REXML::XPath.first( child, "MetaData/IncludeInCompile" ) if !includetag.nil? && includetag.text == "Yes" id = child.attributes["ID"] # passing type, would eventually use to control header/title output type = child.attributes["Type"] @@ -54,6 +55,12 @@ def get_rtf_file_array(scrivpath) return files end def concat(cachefiles,sw_note) $stdout.print "Concatenating #{cachefiles.length} sections to #{sw_note}..." File.open(sw_note,"w"){|f| f.puts cachefiles.map{|s| IO.read(s)} } $stdout.puts " Done." end trap("SIGINT") { exit } unless droplet || debug @@ -63,55 +70,84 @@ def get_rtf_file_array(scrivpath) end end if debug && ARGV.length == 0 path = File.expand_path("~/Dropbox/ScrivTest.scriv") else path = File.expand_path(ARGV[0].gsub(/\/$/,'')) end sw_name = path.gsub(/.*?\/([^\/]+)\.scriv$/,"\\1") sw_target = File.expand_path("~/ScrivWatcher") Dir.mkdir(sw_target) unless File.exists?(sw_target) sw_cache_dir = File.expand_path("~/ScrivWatcher/cache") Dir.mkdir(sw_cache_dir) unless File.exists?(sw_cache_dir) sw_cache = File.expand_path("~/ScrivWatcher/cache/#{sw_name}") # Clear cache if File.exists?(sw_cache) File.delete(*Dir["#{sw_cache}/*"]) Dir.rmdir(sw_cache) end Dir.mkdir(sw_cache) sw_note = "#{sw_target}/ScrivWatcher - #{sw_name}.md" File.delete(sw_note) FileUtils.touch(sw_note) %x{open -a Scrivener "#{path}" && open -a /Applications/Marked.app "#{sw_note}"} first = true files = [] while true do # repeat infinitely unless check_running puts "Marked quit, exiting" exit end notetext = "" # tracking the xml file for ordering changes as well new_xml_time = File.stat(path).mtime.to_i xml_time ||= new_xml_time diff_xml_time = new_xml_time - xml_time unless diff_xml_time == 0 && first == false files = get_rtf_file_array(path) end # track any file changes in folder new_hash = files.collect {|f| [ f['path'], File.stat(f['path']).mtime.to_i ] if f['path'] } # create a hash of timestamps hash ||= new_hash diff_hash = new_hash - hash # check for changes unless first == false && diff_hash.empty? && diff_xml_time == 0 # if changes were found in the timestamps $stdout.puts "************* Change detected..." unless first first = false hash = new_hash xml_time = new_xml_time cachefiles = [] files.each{ |f| if f['path'] cachefile = sw_cache + "/" + File.basename(f['path'],'.rtf') + ".md" if !File.exists?(cachefile) || (File.stat(f['path']).mtime.to_i > File.stat(cachefile).mtime.to_i) # $stdout.puts "Caching #{f['path']}" if debug note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" if titles_as_headers && !f['depth'].nil? f['depth'].times { leader += "#" } leader = "#{leader} #{f['title']}\n\n" end notetext = leader + note + "\n\n" File.open(cachefile,"w"){|cf| cf.puts notetext } end cachefiles.push(cachefile) end } # write the result to the preview file concat(cachefiles,sw_note) end sleep 1 -
ttscoff revised this gist
Jan 27, 2012 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ require 'fileutils' require 'rexml/document' debug = false droplet = true def check_running() newpid = %x{ps ax|grep Marked|grep -v grep|awk '{print $1}'} @@ -74,7 +74,7 @@ def get_rtf_file_array(scrivpath) Dir.mkdir(sw_target) unless File.exists?(sw_target) sw_note = "#{sw_target}/ScrivWatcher - #{sw_name}.md" FileUtils.touch(sw_note) %x{open -a Scrivener "#{path}" && open -a /Applications/Marked.app "#{sw_note}"} first = true -
ttscoff revised this gist
Jan 27, 2012 . 1 changed file with 27 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,7 @@ require 'fileutils' require 'rexml/document' debug = false droplet = false # true if using in a Platypus/Duckbill droplet def check_running() newpid = %x{ps ax|grep Marked|grep -v grep|awk '{print $1}'} @@ -36,15 +37,14 @@ def get_children(ele,path,files,depth) # Take the path to the scrivener file and open the internal XML file. def get_rtf_file_array(scrivpath) scrivpath = File.expand_path(scrivpath) scrivx = File.basename("#{scrivpath}", ".scriv") + ".scrivx" scrivxpath = "#{scrivpath}/#{scrivx}" # handle cases where the package has been renamed after creation unless File.exists?(scrivxpath) scrivxpath = %x{ls -1 #{scrivpath}/*.scrivx|head -n 1}.strip end files = [] doc = REXML::Document.new File.new(scrivxpath) doc.elements.each('ScrivenerProject/Binder/BinderItem') do |ele| if ele.attributes['Type'] == "DraftFolder" get_children(ele,scrivpath,files,1) @@ -55,21 +55,27 @@ def get_rtf_file_array(scrivpath) end trap("SIGINT") { exit } unless droplet || debug if (ARGV.length != 1 || ARGV[0] !~ /\.scriv\/?$/) puts "Usage: scrivwatch.rb /path/to/document.scriv" exit end end if debug path = File.expand_path("~/Dropbox/ScrivTest.scriv") else path = File.expand_path(ARGV[0].gsub(/\/$/,'')) end sw_name = path.gsub(/.*?\/([^\/]+)\.scriv$/,"\\1") sw_target = File.expand_path("~/ScrivWatcher") Dir.mkdir(sw_target) unless File.exists?(sw_target) sw_note = "#{sw_target}/ScrivWatcher - #{sw_name}.md" FileUtils.touch(sw_note) %x{open -a /Applications/Marked.app "#{sw_note}"} first = true while true do # repeat infinitely @@ -79,23 +85,31 @@ def get_rtf_file_array(scrivpath) end notetext = "" files = get_rtf_file_array(path) # track any file changes in folder new_hash = files.collect {|f| [ f['path'], File.stat(f['path']).mtime.to_i ] if f['path'] } # create a hash of timestamps hash ||= new_hash diff_hash = new_hash - hash # check for changes # tracking the xml file for ordering changes as well new_xml_time = File.stat(path).mtime.to_i xml_time ||= new_xml_time diff_xml_time = new_xml_time - xml_time unless first == false && diff_hash.empty? && diff_xml_time == 0 # if changes were found in the timestamps first = false hash = new_hash xml_time = new_xml_time # All attempts to pass more than 1 file to textutil failed. Do it one-at-a-time. files.each{ |f| p f if debug note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" f['depth'].times { leader += "#" } unless f['depth'].nil? notetext = notetext + "#{leader} #{f['title']}\n\n" + note + "\n\n" } # write the result to the preview file watch_note = File.new("#{sw_note}",'w') watch_note.puts notetext watch_note.close end -
ttscoff revised this gist
Jan 26, 2012 . 1 changed file with 37 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,14 +9,26 @@ require 'fileutils' require 'rexml/document' debug = false def check_running() newpid = %x{ps ax|grep Marked|grep -v grep|awk '{print $1}'} return newpid.empty? ? false : true end def get_children(ele,path,files,depth) ele.elements.each('*/BinderItem') do |child| # Ignore docs set to not include in compile included = REXML::XPath.first( child, "MetaData/IncludeInCompile" ).text if included == "Yes" id = child.attributes["ID"] # passing type, would eventually use to control header/title output type = child.attributes["Type"] title = child.elements.to_a[0].text file = "#{path}/Files/Docs/#{id}.rtf" filepath = File.exists?(file) ? file : false files << { 'path' => filepath, 'title' => title, 'depth' => depth, 'type' => type } end get_children(child,path,files,depth+1) end end @@ -26,12 +38,15 @@ def get_rtf_file_array(scrivpath) scrivpath = File.expand_path(scrivpath) files = [] scrivx = File.basename("#{scrivpath}", ".scriv") + ".scrivx" scrivxpath = "#{scrivpath}/#{scrivx}" # handle cases where the package has been renamed after creation unless File.exists?(scrivxpath) scrivxpath = %x{ls -1 #{scrivpath}/*.scrivx|head -n 1}.strip end doc = REXML::Document.new File.new(scrivxpath) doc.elements.each('ScrivenerProject/Binder/BinderItem') do |ele| if ele.attributes['Type'] == "DraftFolder" get_children(ele,scrivpath,files,1) end end @@ -40,8 +55,8 @@ def get_rtf_file_array(scrivpath) end trap("SIGINT") { exit } puts ARGV[0] if (ARGV.length != 1 || ARGV[0] !~ /\.scriv\/?$/) && !debug puts "Usage: scrivwatch.rb /path/to/document.scriv" exit end @@ -50,12 +65,18 @@ def get_rtf_file_array(scrivpath) FileUtils.touch(marked_note) %x{open -a /Applications/Marked.app "#{marked_note}"} if debug path = File.expand_path("~/Dropbox/ScrivTest.scriv") else path = File.expand_path(ARGV[0].gsub(/\/$/,'')) end first = true while true do # repeat infinitely unless check_running puts "Marked quit, exiting" exit end notetext = "" files = get_rtf_file_array(path) new_hash = files.collect {|f| [ f['path'], File.stat(f['path']).mtime.to_i ] if f['path'] } # create a hash of timestamps @@ -67,6 +88,7 @@ def get_rtf_file_array(scrivpath) hash = new_hash # All attempts to pass more than 1 file to textutil failed. Do it one-at-a-time. files.each{ |f| p f if debug note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" f['depth'].times { leader += "#" } @@ -79,4 +101,4 @@ def get_rtf_file_array(scrivpath) end sleep 1 end -
ttscoff revised this gist
Jan 25, 2012 . 1 changed file with 0 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,9 +14,6 @@ def get_children(ele,path,files,depth) ele.elements.each('*/BinderItem') do |child| id = child.attributes["ID"] title = child.elements.to_a[0].text file = "#{path}/Files/Docs/#{id}.rtf" filepath = File.exists?(file) ? file : false files << { 'path' => filepath, 'title' => title, 'depth' => depth } @@ -30,9 +27,6 @@ def get_rtf_file_array(scrivpath) files = [] scrivx = File.basename("#{scrivpath}", ".scriv") + ".scrivx" doc = REXML::Document.new File.new("#{scrivpath}/#{scrivx}") drafts = nil -
ttscoff revised this gist
Jan 25, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ #!/usr/bin/env ruby # scrivwatch.rb by Brett Terpstra, 2011 # Modifications to merge into one file for Markdown viewing in Marked by BooneJS, 2012 # Modified to use REXML and add titles by Brett Terpstra, 2012 <https://gist.github.com/1676667> # # Watch a Scrivener project for changes and update a preview file for Marked # Usage: scrivwatch.rb /path/to/document.scriv -
ttscoff revised this gist
Jan 25, 2012 . 1 changed file with 2 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -54,14 +54,12 @@ def get_rtf_file_array(scrivpath) marked_note = File.expand_path("~/Marked Preview.md") FileUtils.touch(marked_note) %x{open -a /Applications/Marked.app "#{marked_note}"} path = File.expand_path(ARGV[0].gsub(/\/$/,'')) first = true while true do # repeat infinitely notetext = "" @@ -75,7 +73,6 @@ def get_rtf_file_array(scrivpath) hash = new_hash # All attempts to pass more than 1 file to textutil failed. Do it one-at-a-time. files.each{ |f| note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" f['depth'].times { leader += "#" } -
ttscoff created this gist
Jan 25, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,91 @@ #!/usr/bin/env ruby # scrivwatch.rb by Brett Terpstra, 2011 # Modifications to merge into one file for Markdown viewing in Marked by BooneJS, 2012 # Modified to use REXML and add titles by Brett Terpstra, 2012 # # Watch a Scrivener project for changes and update a preview file for Marked # Usage: scrivwatch.rb /path/to/document.scriv # <http://markedapp.com> require 'fileutils' require 'rexml/document' def get_children(ele,path,files,depth) ele.elements.each('*/BinderItem') do |child| id = child.attributes["ID"] title = child.elements.to_a[0].text # child.elements.each('Title') do |title| # sectiontitle = title.text # end file = "#{path}/Files/Docs/#{id}.rtf" filepath = File.exists?(file) ? file : false files << { 'path' => filepath, 'title' => title, 'depth' => depth } get_children(child,path,files,depth+1) end end # Take the path to the scrivener file and open the internal XML file. def get_rtf_file_array(scrivpath) scrivpath = File.expand_path(scrivpath) files = [] scrivx = File.basename("#{scrivpath}", ".scriv") + ".scrivx" # f = File.open("#{scrivpath}/#{scrivx}") # xml = f.read # f.close doc = REXML::Document.new File.new("#{scrivpath}/#{scrivx}") drafts = nil doc.elements.each('ScrivenerProject/Binder/BinderItem') do |ele| if ele.attributes['ID'] == "0" get_children(ele,scrivpath,files,1) end end return files end trap("SIGINT") { exit } if ARGV.length != 1 || ARGV[0] !~ /\.scriv\/?$/ puts "Usage: scrivwatch.rb /path/to/document.scriv" exit end marked_note = File.expand_path("~/Marked Preview.md") FileUtils.touch(marked_note) %x{open -a Marked "#{marked_note}"} path = File.expand_path(ARGV[0].gsub(/\/$/,'')) first = true # puts get_rtf_file_array(path) # exit while true do # repeat infinitely notetext = "" files = get_rtf_file_array(path) new_hash = files.collect {|f| [ f['path'], File.stat(f['path']).mtime.to_i ] if f['path'] } # create a hash of timestamps hash ||= new_hash diff_hash = new_hash - hash # check for changes unless first==false and diff_hash.empty? # if changes were found in the timestamps first = false hash = new_hash # All attempts to pass more than 1 file to textutil failed. Do it one-at-a-time. files.each{ |f| p f['path'] note = f['path'] ? %x{/usr/bin/textutil -convert txt -stdout "#{f['path']}"} : '' leader = "" f['depth'].times { leader += "#" } notetext = notetext + "#{leader} #{f['title']}\n\n" + note + "\n\n" } # write the result to the preview file watch_note = File.new("#{marked_note}",'w') watch_note.puts notetext watch_note.close end sleep 1 end