Skip to content

Instantly share code, notes, and snippets.

@serian
Forked from llatzhar/gist:5208
Created July 31, 2011 16:43
Show Gist options
  • Save serian/1116946 to your computer and use it in GitHub Desktop.
Save serian/1116946 to your computer and use it in GitHub Desktop.
tombloo howm emacs firefox javascript
// for tombloo 0.4.23
/*
filename: YYYY-mm-DD-hhMMss.howm
format:
= title\n
[YYYY-mm-DD hh:MM]\s\n
\n
link
tag
>>
body
<<
description
fix: create_dir
*/
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 "h:\\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);
dir.create(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0664);
} 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.hh + ":";
r += this.mm + "] ";
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());
path += "-" + this.hh + this.mm + this.ss + ".howm";
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.hh = this.fill(today.getHours());
this.mm = this.fill(today.getMinutes());
this.ss = this.fill(today.getSeconds());
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";
text += this.makeDateExp(today) + "\n\n";
text += ps.itemUrl + "\n";
text += ps.tags + "\n";
if (ps.body != null) {
text += ">>\n" +
ps.body.replace(/&nbsp;/ig, "\"")
.replace(/&quot;/ig, "\"")
.replace(/&apos;/ig, "\"")
.replace(/&gt;/ig, "\"")
.replace(/&lt;/ig, "\"")
.replace(/&amp;/ig,"&")
+ "\n<<"
+ "\n\n";
}
text += ps.description + "\n\n";
text += getContents(rfile, 'UTF-8');
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('UTF-8');
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