Class: JekyllImport::Importers::Dotclear

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/dotclear.rb

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.extract_data_section(str) ⇒ Object



34
35
36
# File 'lib/jekyll-import/importers/dotclear.rb', line 34

def self.extract_data_section(str)
  str.gsub(%r!^"!, "").gsub(%r!"$!, "").split('","')
end

.extract_headers_section(str) ⇒ Object



30
31
32
# File 'lib/jekyll-import/importers/dotclear.rb', line 30

def self.extract_headers_section(str)
  str[1..-2].split(" ")[1].split(",")
end

.process(opts) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jekyll-import/importers/dotclear.rb', line 38

def self.process(opts)
  options = {
    :datafile    => opts.fetch("datafile", ""),
    :mediafolder => opts.fetch("mediafolder", ""),
  }

  FileUtils.mkdir_p("_posts")
  FileUtils.mkdir_p("_drafts")

  type_data = ""
  headers = {}
  posts_and_drafts = {}
  keywords = {}

  File.readlines(options[:datafile]).each do |lineraw|
    line = lineraw.strip.gsub(%r!\n$!, "")

    next if line.empty?

    if line.start_with?("[") # post | media \ meta | comment...
      type_data = line.split(" ").first[1..-1]
      headers[type_data] = extract_headers_section(line)
      next
    end

    elts = extract_data_section(line)

    if type_data == "post"
      draft = (elts[headers[type_data].index("post_status")] != "1")

      date_str = elts[headers[type_data].index("post_creadt")]
      date_blank = (date_str.nil? || date_str.empty?)
      date_str_formatted = date_blank ? Date.today : Date.parse(date_str).strftime("%Y-%m-%d")
      title_param = elts[headers[type_data].index("post_title")].to_s.parameterize

      content = elts[headers[type_data].index("post_content_xhtml")].to_s
      content = content.gsub('\"', '"').gsub('\n', "\n").gsub("/public/", "/assets/images/")

      filepath = File.join(Dir.pwd, (draft ? "_drafts" : "_posts"), "#{date_str_formatted}-#{title_param}.html")

      entire_content_file = <<~POST_FILE
        ---
        layout: post
        title: "#{elts[headers[type_data].index("post_title")]}"
        date: #{elts[headers[type_data].index("post_creadt")]} +0100
        tags: ABC
        ---

        #{content}
      POST_FILE

      posts_and_drafts[elts[headers[type_data].index("post_id")]] = { :path => filepath, :content => entire_content_file }
    elsif type_data == "media"
      elts[headers[type_data].index("media_title")]
      mediafilepath = elts[headers[type_data].index("media_file")]

      src_path = File.join(options[:mediafolder], mediafilepath)
      dst_path = File.join(Dir.pwd, "assets", "images", mediafilepath.to_s)

      FileUtils.mkdir_p(File.dirname(dst_path))
      FileUtils.cp(src_path, dst_path)
    elsif type_data == "meta"
      keywords[elts[headers[type_data].index("post_id")]] ||= []
      keywords[elts[headers[type_data].index("post_id")]] << elts[headers[type_data].index("meta_id")]
    elsif type_data == "link"

    elsif type_data == "setting"

    elsif type_data == "comment"

    end
  end

  # POST-process : Change media path in posts and drafts
  posts_and_drafts.each do |post_id, hsh|
    keywords_str = keywords[post_id].to_a.join(", ")
    content_file = hsh[:content]
    content_file = content_file.gsub("tags: ABC", "tags: [#{keywords_str}]")

    File.open(hsh[:path], "wb") do |f|
      f.write(content_file)
    end
  end
end

.require_depsObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jekyll-import/importers/dotclear.rb', line 12

def self.require_deps
  JekyllImport.require_with_fallback(%w(
    rubygems
    fileutils
    safe_yaml
    date
    active_support
    active_support/core_ext/string/inflections
    csv
    pp
  ))
end

.specify_options(c) ⇒ Object



7
8
9
10
# File 'lib/jekyll-import/importers/dotclear.rb', line 7

def self.specify_options(c)
  c.option "datafile", "--datafile PATH", "dotClear export file"
  c.option "mediafolder", "--mediafolder PATH", "dotClear media export folder (media.zip inflated)"
end

.validate(opts) ⇒ Object



25
26
27
28
# File 'lib/jekyll-import/importers/dotclear.rb', line 25

def self.validate(opts)
  abort "Specify a data file !" if opts["datafile"].nil? || opts["datafile"].empty?
  abort "Specify a media folder !" if opts["mediafolder"].nil? || opts["mediafolder"].empty?
end