2013-11-22 40 views
1

這裏的總菜鳥。紅寶石 - URL到Markdown

我工作的定製由Brett特普斯特拉做一個腳本 - http://brettterpstra.com/2013/11/01/save-pocket-favorites-to-nvalt-with-ifttt-and-hazel/

煤礦是不同的用途:我想用一個特定的標籤保存我的通知板的書籤到一個文件中的Dropbox在降價。

我給它一個文本文件如:

Title: Yesterday is over. 
URL: http://www.jonacuff.com/blog/want-to-change-the-world-get-doing/ 
Tags: 2md, 2wcx, 2pdf 
Date: June 20, 2013 at 06:20PM 
Image: notused 
Excerpt: You can't start the next chapter of your life if you keep re-reading the last one. 

,並將其輸出的降價文件。

除了'摘錄'(見上文)不止一行時,一切都很好。有時候是幾段。當發生這種情況時,它停止工作。當我從命令行輸入時,它仍然在等待更多輸入。

下面是它不工作的一個文件的一個例子:

Title: Talking ’bout my Generation. 
URL: http://blog.greglaurie.com/?p=8881 
Tags: 2md, 2wcx, 2pdf 
Date: June 28, 2013 at 09:46PM 
Image: notused 
Excerpt: Contrast two men from the 19th century: Max Jukes and Jonathan Edwards. 

Max Jukes lived in New York. He did not believe in Christ or in raising his children in the way of the Lord. He refused to take his children to church, even when they asked to go. Of his 1,026 descendants: 
•300 were sent to prison for an average term of 13 years 
•190 were prostitutes 
•680 were admitted alcoholics 
His family, thus far, has cost the state in excess of $420,000 and has made no contribution to society. 

Jonathan Edwards also lived in New York, at the same time as Jukes. He was known to have studied 13 hours a day and, in spite of his busy schedule of writing, teaching, and pastoring, he made it a habit to come home and spend an hour each day with his children. He also saw to it that his children were in church every Sunday. Of his 929 descendants: 
•430 were ministers 
•86 became university professors 
•13 became university presidents 
•75 authored good books 
•7 were elected to the United States Congress 
•1 was Vice President of the United States 
Edwards’ family never cost the state one cent. 

We tend to think that our decisions only affect ourselves, but they have ramifications for generations to come. 

這裏是什麼樣子後,我運行命令的截圖:https://www.dropbox.com/s/i9zg483k7nkdp6f/Screenshot%202013-11-22%2016.39.17.png 我希望這件事情很容易。有任何想法嗎?


#!/usr/bin/env ruby 
# Works with IFTTT recipe https://ifttt.com/recipes/125999 
# 
# Set Hazel to watch the folder you specify in the recipe. 
# Make sure nvALT is set to store its notes as individual files. 
# Edit the $target_folder variable below to point to your nvALT 
# ntoes folder. 
require 'date' 
require 'open-uri' 
require 'net/http' 
require 'fileutils' 
require 'cgi' 

$target_folder = "~/Dropbox/messx/urls2md" 

def url_to_markdown(url) 
    res = Net::HTTP.post_form(URI.parse("http://heckyesmarkdown.com/go/"),{'u'=>url,'read'=>'1'}) 
    if res.code.to_i == 200 
     res.body 
    else 
     false 
    end 
end 

file = ARGV[0] 

begin 
    input = IO.read(file).force_encoding('utf-8') 

    headers = {} 
    input.each_line {|line| 
     key, value = line.split(/: /) 
     headers[key] = value.strip || "" 
    } 

    outfile = File.join(File.expand_path($target_folder), headers['Title'].gsub(/["!*?'|]/,'') + ".txt") 

    date = Time.now.strftime("%Y-%m-%d %H:%M") 
    date_added = Date.parse(headers['Date']).strftime("%Y-%m-%d %H:%M") 
    content = "Title: #{headers['Title']}\nDate: #{date}\nDate Added: #{date_added}\nSource: #{headers['URL']}\n" 

    tags = false 
    if headers['Tags'].length > 0 
     tag_arr = header s['Tags'].split(", ") 
     tag_arr.map! {|tag| 
      %Q{"#{tag.strip}"} 
     } 
     tags = tag_arr.join(" ") 
     content += "Keywords: #{tags}\n" 
    end 

    markdown = url_to_markdown(headers['URL']).force_encoding('utf-8') 

    if markdown 
     content += headers['Image'].length > 0 ? "\n\n> #{headers['Excerpt']}\n\n---#{markdown}\n" : "\n\n"+markdown 
    else 
     content += headers['Image'].length > 0 ? "\n\n![](#{headers['Image']})\n\n#{headers['Excerpt']}\n" : "\n\n"+headers['Excerpt'] 
    end 

    File.open(outfile,'w') {|f| 
     f.puts content 
    } 

    if tags && File.exists?("/usr/local/bin/openmeta") 
     %x{/usr/local/bin/openmeta -a #{tags} -p "#{outfile}"} 
    end 
    # FileUtils.rm(file) 
rescue Exception => e 
    puts e 
end 
+0

你有一個破解它的示例輸入文件嗎?我添加了一個額外的「摘錄:」行,並完成。你編寫它的方式,它吹走了以前的「摘錄:」一行,但它並沒有掛起。 –

+0

感謝您的迴應!我只是添加了一個示例文件和我試圖運行時的樣子截圖。 –

+0

我想這個問題與摘錄的長度沒有關係。而問題在於節選中的結腸。嘗試用單引號括住文字,一切都可能會很好。 – mudasobwa

回答

0

這個怎麼樣?相應地修改input.each_line面積:

headers = {} 
key = nil 
input.each_line do |line| 
    match = /^(?<key>\w+)\s*:\s*(?<value>.*)/.match(line) 
    value = line 
    if match 
    key = match[:key].strip 
    headers[key] = match[:value].strip 
    else 
    headers[key] += line 
    end 
end 

首先,拆分對剛剛「:」是危險的,因爲這可以在內容。相反,(從代碼簡化)/^\w+:.*/的正則表達式將匹配「Word:Content」。由於「摘錄:」後面的行不加前綴,因此您需要掛在上次看到的關鍵字上,並且只要沒有該行的關鍵字即可附加。您可能需要在那裏添加一個換行符,具體取決於您使用該頭信息所做的操作,但似乎可行。

+0

是的!!!這工作!儘管我遇到了不同的錯誤。不確定它是否相關。如果'title'字段有斜線(EG:標題:Harvest/Riverside和Orange County的本週末),它會再次錯誤地輸出:(截圖)https://www.dropbox.com/s/fou21inpvfv6vsp/Scr​​eenshot %202013-11-23%2019.18.44.png - 再次感謝您的幫助,我仍然有這麼多的學習! :) –

+0

你需要改變你的'headers''Title']。gsub(/ [「!*?'|] /,'')+」.txt「'還可以刪除'/'字符 –

+1

'header ['Title'] .gsub(/ [「!*?'| \/\\] /,'')+」.txt「)' - 完美地工作!再次感謝尼克! –