Skip to content

Instantly share code, notes, and snippets.

@fbiville
Last active August 29, 2015 14:20
Show Gist options
  • Save fbiville/45931af939672eefb121 to your computer and use it in GitHub Desktop.
Save fbiville/45931af939672eefb121 to your computer and use it in GitHub Desktop.
(Very) quick'n'dirty way to convert Dotclear posts to Asciidoc (for Hubpress)
package net.biville.florent;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.Files.write;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Migration {
public static void main(String[] args) throws IOException {
String pandocPath = "/usr/local/bin/pandoc";
String postDumpLocation = "/dotclear_post.json";
try (InputStream stream = Migration.class.getResourceAsStream(postDumpLocation);
Reader reader = new InputStreamReader(stream)) {
Collection<Post> posts = new Gson().fromJson(reader, new TypeToken<Collection<Post>>(){}.getType());
File blogDirectory = Files.createTempDirectory("blog").toFile();
String absolutePath = blogDirectory.getAbsolutePath();
Collection<String> scriptContents = new ArrayList<>();
scriptContents.add("#!/bin/bash");
scriptContents.add(String.format("cd %s;", absolutePath));
scriptContents.addAll(posts.stream().flatMap(
(post -> {
try {
String datePrefix = post.getPost_dt().substring(0, 4 + 1 + 2 + 1 + 2);
String[] urlParts = post.getPost_url().split("\\/");
String title = urlParts[urlParts.length - 1];
File file = new File(blogDirectory, title + ".html");
write(Paths.get(file.toURI()), post.getPost_content_xhtml().getBytes(UTF_8));
return asList(
String.format(
pandocPath + " '%s.html' -f html -t asciidoc -s -o '%s-%s.adoc'",
title, datePrefix, title
),
String.format("rm '%s.html'", title)
).stream();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
})
).collect(toList()));
File scriptFile = new File(blogDirectory, "migrate.sh");
String fileName = scriptFile.getName();
write(scriptFile.toPath(), scriptContents, UTF_8);
System.out.println(String.format(
"cd %s && chmod u+x %s && ./%s",
blogDirectory.getAbsolutePath(),
fileName,
fileName
));
}
}
}
package net.biville.florent;
public class Post {
private String post_dt;
private String post_title;
private String post_content_xhtml;
private String post_url;
Post() {}
public String getPost_dt() {
return post_dt;
}
public void setPost_dt(String post_dt) {
this.post_dt = post_dt;
}
public String getPost_title() {
return post_title;
}
public void setPost_title(String post_title) {
this.post_title = post_title;
}
public String getPost_content_xhtml() {
return post_content_xhtml;
}
public void setPost_content_xhtml(String post_content_xhtml) {
this.post_content_xhtml = post_content_xhtml;
}
public String getPost_url() {
return post_url;
}
public void setPost_url(String post_url) {
this.post_url = post_url;
}
}
@fbiville
Copy link
Author

fbiville commented May 3, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment