Module: JekyllImport::Importers::Blogger::BloggerAtomStreamListenerMethods

Defined in:
lib/jekyll-import/importers/blogger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commentsObject

Returns the value of attribute comments.



105
106
107
# File 'lib/jekyll-import/importers/blogger.rb', line 105

def comments
  @comments
end

#leave_blogger_infoObject

Returns the value of attribute leave_blogger_info.



105
106
107
# File 'lib/jekyll-import/importers/blogger.rb', line 105

def leave_blogger_info
  @leave_blogger_info
end

#original_url_baseObject (readonly)

Returns the value of attribute original_url_base.



106
107
108
# File 'lib/jekyll-import/importers/blogger.rb', line 106

def original_url_base
  @original_url_base
end

Instance Method Details

#get_post_data_from_in_entry_elem_infoObject



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/jekyll-import/importers/blogger.rb', line 229

def get_post_data_from_in_entry_elem_info
  if @in_entry_elem.nil? || !@in_entry_elem.key?(:meta) || !@in_entry_elem[:meta].key?(:kind)
    nil
  elsif @in_entry_elem[:meta][:kind] == "post"
    timestamp = Time.parse(@in_entry_elem[:meta][:published]).strftime("%Y-%m-%d")
    if @in_entry_elem[:meta][:original_url]
      original_uri = URI.parse(@in_entry_elem[:meta][:original_url])
      original_path = original_uri.path.to_s
      filename = format("%s-%s", timestamp, File.basename(original_path, File.extname(original_path)))

      @original_url_base = "#{original_uri.scheme}://#{original_uri.host}"
    elsif @in_entry_elem[:meta][:draft]
      # Drafts don't have published urls
      name = @in_entry_elem[:meta][:title]
      filename = if name.nil?
                   timestamp
                 else
                   format("%s-%s", timestamp, CGI.escape(name.downcase.tr("+/\\:'\"<>{}?%*|.", "-")))
                 end
    else
      raise "Original URL is missing"
    end

    header = {
      "layout" => "post",
      "title"  => @in_entry_elem[:meta][:title],
      "date"   => @in_entry_elem[:meta][:published],
      "author" => @in_entry_elem[:meta][:author],
      "tags"   => @in_entry_elem[:meta][:category],
    }
    header["modified_time"] = @in_entry_elem[:meta][:updated] if @in_entry_elem[:meta][:updated] && @in_entry_elem[:meta][:updated] != @in_entry_elem[:meta][:published]
    header["thumbnail"] = @in_entry_elem[:meta][:thumbnail] if @in_entry_elem[:meta][:thumbnail]
    header["blogger_id"] = @in_entry_elem[:meta][:id] if @leave_blogger_info
    header["blogger_orig_url"] = @in_entry_elem[:meta][:original_url] if @leave_blogger_info && @in_entry_elem[:meta][:original_url]

    body = @in_entry_elem[:body]

    # body escaping associated with liquid
    if body =~ %r!{{!
      body.gsub!(%r!{{!, '{{ "{{" }}')
    end
    if body =~ %r!{%!
      body.gsub!(%r!{%!, '{{ "{%" }}')
    end

    { :filename => filename, :header => header, :body => body }
  elsif @in_entry_elem[:meta][:kind] == "comment"
    timestamp = Time.parse(@in_entry_elem[:meta][:published]).strftime("%Y-%m-%d")
    if @in_entry_elem[:meta][:original_url]
      unless @comment_seq
        @comment_seq = 1
      end

      original_uri = URI.parse(@in_entry_elem[:meta][:original_url])
      original_path = original_uri.path.to_s
      filename = format("%s-%s-%s", timestamp, File.basename(original_path, File.extname(original_path)), @comment_seq)

      @comment_seq += 1

      @original_url_base = "#{original_uri.scheme}://#{original_uri.host}"
    else
      raise "Original URL is missing"
    end

    header = {
      "date"            => @in_entry_elem[:meta][:published],
      "author"          => @in_entry_elem[:meta][:author],
      "blogger_post_id" => @in_entry_elem[:meta][:post_id],
    }
    header["modified_time"] = @in_entry_elem[:meta][:updated] if @in_entry_elem[:meta][:updated] && @in_entry_elem[:meta][:updated] != @in_entry_elem[:meta][:published]
    header["thumbnail"] = @in_entry_elem[:meta][:thumbnail] if @in_entry_elem[:meta][:thumbnail]
    header["blogger_id"] = @in_entry_elem[:meta][:id] if @leave_blogger_info
    header["blogger_orig_url"] = @in_entry_elem[:meta][:original_url] if @leave_blogger_info && @in_entry_elem[:meta][:original_url]

    body = @in_entry_elem[:body]

    # body escaping associated with liquid
    if body =~ %r!{{!
      body.gsub!(%r!{{!, '{{ "{{" }}')
    end
    if body =~ %r!{%!
      body.gsub!(%r!{%!, '{{ "{%" }}')
    end

    { :filename => filename, :header => header, :body => body }
  end
end

#tag_end(tag) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/jekyll-import/importers/blogger.rb', line 181

def tag_end(tag)
  case tag
  when "entry"
    raise "nest entry element" unless @in_entry_elem

    if @in_entry_elem[:meta][:kind] == "post"
      post_data = get_post_data_from_in_entry_elem_info

      if post_data
        target_dir = "_posts"
        target_dir = "_drafts" if @in_entry_elem[:meta][:draft]

        FileUtils.mkdir_p(target_dir)

        file_name = URI.decode("#{post_data[:filename]}.html")
        File.open(File.join(target_dir, file_name), "w") do |f|
          f.flock(File::LOCK_EX)

          f << post_data[:header].to_yaml
          f << "---\n\n"
          f << post_data[:body]
        end
      end
    elsif @in_entry_elem[:meta][:kind] == "comment" && @comments
      post_data = get_post_data_from_in_entry_elem_info

      if post_data
        target_dir = "_comments"

        FileUtils.mkdir_p(target_dir)

        file_name = URI.decode("#{post_data[:filename]}.html")
        File.open(File.join(target_dir, file_name), "w") do |f|
          f.flock(File::LOCK_EX)

          f << post_data[:header].to_yaml
          f << "---\n\n"
          f << post_data[:body]
        end
      end
    end

    @in_entry_elem = nil
  end

  @tag_bread.pop
end

#tag_start(tag, attrs) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/jekyll-import/importers/blogger.rb', line 108

def tag_start(tag, attrs)
  @tag_bread = [] unless @tag_bread
  @tag_bread.push(tag)

  case tag
  when "entry"
    raise "nest entry element" if @in_entry_elem
    @in_entry_elem = { :meta => {}, :body => nil }
  when "title"
    if @in_entry_elem
      raise 'only <title type="text"></title> is supported' if attrs["type"] != "text"
    end
  when "category"
    if @in_entry_elem
      if attrs["scheme"] == "http://www.blogger.com/atom/ns#"
        @in_entry_elem[:meta][:category] = [] unless @in_entry_elem[:meta][:category]
        @in_entry_elem[:meta][:category] << attrs["term"]
      elsif attrs["scheme"] == "http://schemas.google.com/g/2005#kind"
        kind = attrs["term"]
        kind.sub!(Regexp.new("^http://schemas\\.google\\.com/blogger/2008/kind\\#"), "")
        @in_entry_elem[:meta][:kind] = kind
      end
    end
  when "content"
    if @in_entry_elem
      @in_entry_elem[:meta][:content_type] = attrs["type"]
    end
  when "link"
    if @in_entry_elem
      if attrs["rel"] == "alternate" && attrs["type"] == "text/html"
        @in_entry_elem[:meta][:original_url] = attrs["href"]
      elsif attrs["rel"] == "replies" && attrs["type"] == "text/html"
        unless @in_entry_elem[:meta][:original_url]
          @in_entry_elem[:meta][:original_url] = attrs["href"].sub(%r!\#comment-form$!, "")
        end
      end
    end
  when "media:thumbnail"
    if @in_entry_elem
      @in_entry_elem[:meta][:thumbnail] = attrs["url"]
    end
  when "thr:in-reply-to"
    if @in_entry_elem
      @in_entry_elem[:meta][:post_id] = attrs["ref"]
    end
  end
end

#text(text) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/jekyll-import/importers/blogger.rb', line 156

def text(text)
  if @in_entry_elem
    case @tag_bread.last
    when "id"
      @in_entry_elem[:meta][:id] = text
    when "published"
      @in_entry_elem[:meta][:published] = text
    when "updated"
      @in_entry_elem[:meta][:updated] = text
    when "title"
      @in_entry_elem[:meta][:title] = text
    when "content"
      @in_entry_elem[:body] = text
    when "name"
      if @tag_bread[-2..-1] == %w(author name)
        @in_entry_elem[:meta][:author] = text
      end
    when "app:draft"
      if @tag_bread[-2..-1] == %w(app:control app:draft)
        @in_entry_elem[:meta][:draft] = true if text == "yes"
      end
    end
  end
end