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
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
155
156
157
158
159
160
161
162
|
# File 'lib/jekyll-import/importers/drupal_common.rb', line 49
def process(options)
engine = options.fetch("engine", DEFAULTS["engine"])
dbname = options.fetch("dbname")
user = options.fetch("user")
pass = options.fetch("password", DEFAULTS["password"])
host = options.fetch("host", DEFAULTS["host"])
port = options.fetch("port", DEFAULTS["port"])
prefix = options.fetch("prefix", DEFAULTS["prefix"])
types = options.fetch("types", DEFAULTS["types"])
db = if engine == "postgresql"
Sequel.postgres(dbname, :user => user, :password => pass, :host => host, :encoding => "utf8")
else
Sequel.mysql2(dbname, :user => user, :password => pass, :host => host, :port => port, :encoding => "utf8")
end
query = build_query(prefix, types, engine)
conf = Jekyll.configuration({})
src_dir = conf["source"]
dirs = {
:_aliases => src_dir,
:_posts => File.join(src_dir, "_posts").to_s,
:_drafts => File.join(src_dir, "_drafts").to_s,
:_layouts => Jekyll.sanitized_path(src_dir, conf["layouts_dir"].to_s),
}
dirs.each do |_key, dir|
FileUtils.mkdir_p dir
end
File.open(File.join(dirs[:_layouts], "refresh.html"), "w") do |f|
f.puts " <!DOCTYPE html>\n <html>\n <head>\n <meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n <meta http-equiv=\"refresh\" content=\"0;url={{ page.refresh_to_post_id }}.html\" />\n <link rel=\"canonical\" href=\"{{ page.refresh_to_post_id }}.html\" />\n </head>\n </html>\n HTML\n end\n\n db[query].each do |post|\n # Get required fields\n data, content = post_data(post)\n\n data[\"layout\"] = post[:type]\n title = data[\"title\"] = post[:title].strip.force_encoding(\"UTF-8\")\n time = data[\"created\"] = post[:created]\n\n # Get the relevant fields as a hash and delete empty fields\n data = data.delete_if { |_k, v| v.nil? || v == \"\" }.each_pair do |_k, v|\n v.is_a?(String) ? v.force_encoding(\"UTF-8\") : v\n end\n\n # Construct a Jekyll compatible file name\n is_published = post[:status] == 1\n node_id = post[:nid]\n dir = is_published ? dirs[:_posts] : dirs[:_drafts]\n slug = title.strip.downcase.gsub(%r!(&|&)!, \" and \").gsub(%r![\\s\\.\\/\\\\]!, \"-\").gsub(%r![^\\w-]!, \"\").gsub(%r![-_]{2,}!, \"-\").gsub(%r!^[-_]!, \"\").gsub(%r![-_]$!, \"\")\n filename = Time.at(time).to_datetime.strftime(\"%Y-%m-%d-\") + slug + \".md\"\n\n # Write out the data and content to file\n File.open(\"\#{dir}/\#{filename}\", \"w\") do |f|\n f.puts data.to_yaml\n f.puts \"---\"\n f.puts content\n end\n\n # Make a file to redirect from the old Drupal URL\n next unless is_published\n\n alias_query = aliases_query(prefix)\n type = post[:type]\n\n aliases_type = db[alias_query, \"\#{type}/\#{node_id}\"].all\n aliases_node = db[alias_query, \"node/\#{node_id}\"].all\n aliases = aliases_type.concat aliases_node\n\n aliases.push(:alias => \"\#{type}/\#{node_id}\")\n aliases.push(:alias => \"node/\#{node_id}\")\n\n aliases.each do |url_alias|\n redirect_prefix = \"\"\n categories = data[\"categories\"]\n unless categories.nil? || categories.length.zero?\n first_category = categories[0]\n redirect_prefix = \"\#{first_category}/\"\n end\n\n partition = url_alias[:alias].rpartition(\"/\")\n dir = \"\"\n file = partition.last\n\n if partition.first.length.positive?\n dir = \"\#{partition.first}/\"\n FileUtils.mkdir_p \"\#{dirs[:_aliases]}/\#{dir}\"\n end\n\n File.open(\"\#{dirs[:_aliases]}/\#{dir}\#{file}.md\", \"w\") do |f|\n f.puts \"---\"\n f.puts \"layout: refresh\"\n f.puts \"permalink: \#{dir}\#{file}/\"\n f.puts \"refresh_to_post_id: /\#{redirect_prefix}\#{Time.at(time).to_datetime.strftime(\"%Y/%m/%d/\") + slug}\"\n f.puts \"---\"\n end\n end\n end\nend\n"
|