class Clippings::Parse
解析
Public Class Methods
new(filepath)
click to toggle source
# File lib/clippings.rb, line 16 def initialize(filepath) @filepath = filepath end
Public Instance Methods
parseForFragment(sections = nil)
click to toggle source
解析为片段
# File lib/clippings.rb, line 41 def parseForFragment(sections = nil) sections ||= parseForSection() fragments = [] sections.each do |section| fragment = {} fragment[:title_author], fragment[:type_date], fragment[:content] = section.split("\r", 3) # 提取片段的标题与作者 /(.*)\((.*)\).*/ =~ fragment[:title_author] fragment[:title] = $1 || fragment[:title_author] fragment[:author] = $2 || NOTE_DEFAULT_AUTHOR fragment.delete(:title_author) # 提取片段的类型与创建日期 /(#{NOTE_LABEL}|#{HIGHLIGHT_LABEL}).*(#{ADD_LABEL})(.*)/ =~ (fragment[:type_date]) fragment[:type] = ((HIGHLIGHT_LABEL.include? $1) ? :highlight : :note) fragment[:date] = $3 || NOTE_DEFAULT_DATETIME fragment.delete(:type_date) fragment[:content].lstrip! # 将片段添加到数组 fragments.push(fragment) end return fragments end
parseForNote(fragments = nil)
click to toggle source
解析为笔记
# File lib/clippings.rb, line 68 def parseForNote(fragments = nil) fragments ||= parseForFragment() @notes = {} fragments.each do |fragment| title = fragment[:title].strip author = fragment[:author] fragment.delete(:title) fragment.delete(:author) if @notes.has_key?(title) @notes[title][:fragment].push(fragment) else @notes[title] = { :title=>title, :author=>author, :fragment =>[fragment] } end end return @notes end
parseForSection()
click to toggle source
解析为块
# File lib/clippings.rb, line 21 def parseForSection sections = [] if File.exist?(@filepath) section = '' File.open(@filepath, 'r') do |file| file.each_line do |line| if line.start_with?(NOTE_SECTION_SEPARATOR) sections.push(section) section = '' else section.concat(line) end end end end sections.delete_at(0) return sections end