%%%-------------------------------------------------------------------
%%% File : to_html.erl
%%% Author : M.W. Park <manywaypark@gmail.com>
%%% Description : converts delicious bookmark data file into html file for import.
%%% 1. install sqlite-erlang3(https://github.com/alexeyr/erlang-sqlite3).
%%% 2. copy ybookmarks.sqlite as delicious.db in current dir.
%%% 3. elrc to_html.erl
%%% 4. erl -noshell -pa [path/to/sqlite-erlang/ebin/] -run to_html doIt -run init stop
%%% 5. import output file(delicious.html) at http://export.delicious.com/settings/bookmarks/import
%%%
%%% Known Problems (for now) :
%%% 1. Hangul(Korean letters) is ignored by delicious (i registered ths issue).
%%% 2. UTF-8 encoded urls are not working (an odd 25 is added(%EC->%25EC) when delicious redirects it, so can't connect to the right url).
%%%
%%% Created : 2011-12-09 Friday by M.W. Park
%%%-------------------------------------------------------------------
-module(to_html).
-export([doIt/0]).
doIt() ->
{ok,Output}=file:open("delicious.html",[write]),
io:format(Output, "~s~n", [prefix()]),
sqlite3:open(delicious), % open 'delicious.db'
[{columns, _Columns}, {rows, Bookmarks}] = sqlite3:sql_exec(delicious, "select rowid,name,url,added_date,shared,description from bookmarks;"), % for all bookmarks
lists:foreach(fun(Bookmark) ->
%%io:format("row=~p~n", [Bookmark]),
io:format(Output, "~s~n", [xmerl_ucs:to_utf8(bookmark(Bookmark))])
end,
Bookmarks),
sqlite3:close(delicious),
io:format(Output, "~s~n", [postfix()]),
file:close(Output).
bookmark(Bookmark) ->
{RowId, Name, Url, Date, Shared, Desc} = Bookmark,
io_lib:format("<DT><A HREF=\"~s\" ADD_DATE=\"~ts\" PRIVATE=\"~p\" TAGS=\"~ts\">~ts</A>
<DD>~ts", [Url, re:replace(integer_to_list(Date), "000000$", "", [{return,list}]), if <<"true">> =:= Shared -> 0; true -> 1 end, get_tags(RowId), xmerl_ucs:from_utf8(Name), xmerl_ucs:from_utf8(Desc)]).
get_tags(RowId) ->
Q = io_lib:format("select * from tags where rowid in (select tag_id from bookmarks_tags where bookmark_id=~p);", [RowId]),
[{columns, _Columns}, {rows, Tags}] = sqlite3:sql_exec(delicious, Q),
string:join(lists:map(fun({Tag}) -> xmerl_ucs:from_utf8(Tag) end, Tags), ",").
prefix() ->
io_lib:format("<!DOCTYPE NETSCAPE-Bookmark-file-1>~n"
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">~n"
"<!-- This is an automatically generated file.~n"
"It will be read and overwritten.~n"
"Do Not Edit! -->~n"
"<TITLE>Bookmarks</TITLE>~n"
"<H1>Bookmarks</H1>~n"
"<DL><p>", []).
postfix() ->
io_lib:format("</DL><p>", []).