Class: Everdone::EnmlFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/everdone/enmlformatter.rb

Constant Summary collapse

URL_REGEX =

from: code.tutsplus.com/tutorials/8-regular-expressions-you-should-know–net-6149 match only url’s that take the form http://…, https://… but NOT <whatever>@<url> or <host>.<domain>.<tld>

/((https?:\/\/)([\da-zA-Z\.-]+)\.([a-z\.]{2,6})(:[\d]+)?([\/\w \.?%,_&=+-]*)*\/?)/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ EnmlFormatter

Returns a new instance of EnmlFormatter.



14
15
16
17
# File 'lib/everdone/enmlformatter.rb', line 14

def initialize(config)
    @config = config
    @body = ""
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/everdone/enmlformatter.rb', line 12

def body
  @body
end

Instance Method Details

#clearObject



91
92
93
# File 'lib/everdone/enmlformatter.rb', line 91

def clear
    @body = ""
end

#datetime(text, source_format) ⇒ Object



86
87
88
89
# File 'lib/everdone/enmlformatter.rb', line 86

def datetime(text, source_format)
    @body = @body + self.datetime_to_string(text, source_format)
    return self
end

#datetime_to_string(text, source_format) ⇒ Object



82
83
84
# File 'lib/everdone/enmlformatter.rb', line 82

def datetime_to_string(text, source_format)
    return DateTime.strptime(text, source_format).new_offset(DateTime.now.offset).strftime(@config.evernote_datetime_format)
end

#h1(text) ⇒ Object



57
58
59
60
# File 'lib/everdone/enmlformatter.rb', line 57

def h1(text)
    @body = @body + "<h1>#{text}</h1>"
    return self
end

#h2(text) ⇒ Object



62
63
64
65
# File 'lib/everdone/enmlformatter.rb', line 62

def h2(text)
    @body = @body + "<h2>#{text}</h2>"
    return self
end

#h3(text) ⇒ Object



67
68
69
70
# File 'lib/everdone/enmlformatter.rb', line 67

def h3(text)
    @body = @body + "<h3>#{text}</h3>"
    return self
end


77
78
79
80
# File 'lib/everdone/enmlformatter.rb', line 77

def link(text, url)
    @body = @body + "<a href='#{url}'>#{text}</a>"
    return self
end

#newlineObject



52
53
54
55
# File 'lib/everdone/enmlformatter.rb', line 52

def newline()
    @body = @body + "<br/>"
    return self
end

#rawtext(text) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/everdone/enmlformatter.rb', line 28

def rawtext(text)
    # Take text and do some cooking
    #
    # Escape HTML tags so Evernote doesn't freak out on them
    text = CGI::escapeHTML(text)
    # Remove newlines and insert HTML breaks (<br/>)
    text_lines = text.split(/\n/)
    text = ""
    text_lines.each { |line|  
        # Find URL-looking text and turn it into a link
        url_match = line.match(URL_REGEX)
        if url_match
            url = url_match[1]
            line.gsub!(url, "<a href='#{url}'>#{url}</a>") if url
        end
        text = text + "#{line}<br/>"
    }
    # Fix up some Todoist crap: It’s -> It's
    text.gsub!("’", "'");
    # Put it in the body
    @body = @body + text
    return self
end

#spaceObject



72
73
74
75
# File 'lib/everdone/enmlformatter.rb', line 72

def space
    @body = @body + "&nbsp;"
    return self
end

#text(text) ⇒ Object



19
20
21
22
# File 'lib/everdone/enmlformatter.rb', line 19

def text(text)
    @body = @body + text
    return self
end

#to_sObject



95
96
97
98
99
100
# File 'lib/everdone/enmlformatter.rb', line 95

def to_s
    ret = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
    ret = "<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml2.dtd\">"
    ret += "<en-note>#{@body}</en-note>"
    return ret
end