Skip to content

Instantly share code, notes, and snippets.

@llatzhar
Created August 13, 2008 06:30
Show Gist options
  • Save llatzhar/5208 to your computer and use it in GitHub Desktop.
Save llatzhar/5208 to your computer and use it in GitHub Desktop.
// for tombloo 0.3.13
models.register(
{
name : 'Howm',
ICON : 'chrome://tombloo/skin/local.ico',
check : function(ps) {
switch (ps.type) {
case 'regular':
case 'quote':
case 'link':
return true;
}
},
fill : function(value) {
var r = "";
if (value < 10) {
r += "0";
}
r += value;
return r;
},
howm_path : function() {
return "d:\\home\\howm\\";
},
year_path : function(today) {
return this.howm_path() + (today.getYear() + 1900) + "\\";
},
month_path : function(today) {
return this.year_path(today) + this.fill(today.getMonth() + 1) + "\\";
},
create_dir : function(path) {
var dir = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
dir.initWithPath(path);
try {
dir.create(nsIFile::DIRECTORY_TYPE, 0644);
} catch(e) {
return false;
}
return true;
},
makeDateExp : function(today) {
r = "[" + (1900 + today.getYear()) + "-";
r += this.fill(today.getMonth() + 1) + "-";
r += this.fill(today.getDate()) + " ";
r += this.fill(today.getHours()) + ":";
r += this.fill(today.getHours()) + ":";
r += this.fill(today.getMinutes()) + "]";
return r;
},
file_path : function(ps, today) {
var path = this.month_path(today);
path += today.getYear() + 1900 + "-";
path += this.fill(today.getMonth() + 1) + "-";
path += this.fill(today.getDate());
switch (ps.type) {
case 'regular':
path += "Regular.howm";
break;
case 'quote':
path += "Quote__.howm";
break;
case 'link':
path += "Link___.howm";
break;
default:
path += "Regular.howm";
break;
}
return path;
},
ensureYearDir : function(today) {
return this.create_dir(this.year_path(today));
},
ensureMonthDir : function(today) {
return this.create_dir(this.month_path(today));
},
post : function(ps){
var today = new Date();
this.ensureYearDir(today);
this.ensureMonthDir(today);
var rfile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
rfile.initWithPath(this.file_path(ps, today));
if (ps.item == "") {
ps.item = "untitled";
}
var text = "= " + ps.item + "\n\n";
text += ps.itemUrl + "\n";
text += ps.tags + "\n";
if (ps.body != null) {
text += ps.body.replace(/&nbsp;/ig, "\"")
.replace(/&quot;/ig, "\"")
.replace(/&apos;/ig, "\"")
.replace(/&gt;/ig, "\"")
.replace(/&lt;/ig, "\"")
.replace(/&amp;/ig,"&") + "\n\n";
}
text += ps.description + "\n\n";
text += this.makeDateExp(today) + "\n\n\n";
text += getContents(rfile, 'Shift-JIS');
var wfile = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
wfile.initWithPath(this.file_path(ps, today));
stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
stream.init(wfile, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
text = text.convertFromUnicode('Shift-JIS');
stream.write(text, text.length);
stream.close();
return succeed();
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment