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.



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

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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

Instance Method Details

#clearObject



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

def clear
    @body = ""
end

#datetime(text, source_format) ⇒ Object



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

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

#datetime_to_string(text, source_format) ⇒ Object



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

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



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

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

#h2(text) ⇒ Object



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

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

#h3(text) ⇒ Object



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

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


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

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

#newlineObject



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

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

#rawtext(text) ⇒ Object



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

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



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

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

#text(text) ⇒ Object



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

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

#to_sObject



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

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