Skip to content

Commit

Permalink
add replaceImageToAttached
Browse files Browse the repository at this point in the history
  • Loading branch information
sungkwangsong committed Aug 8, 2014
1 parent 5ef9d87 commit f6618ed
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 32 deletions.
12 changes: 8 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
0.1.1
=====
## feature
- **replaceImageToAttached** : replace image path to tistory attached image url

0.1.0
======

## feature

- *blog* : info API implementations
- *post* : list, read, write, update, attach API implemenations
- *category* : list API and find function implementations
- **blog** : info API implementations
- **post** : list, read, write, update, attach API implemenations
- **category** : list API and find function implementations
52 changes: 37 additions & 15 deletions bin/tistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var path = require('path'),
nopt = require("nopt");

var knownOpts = {
"help":Boolean,
"help": Boolean,

"targetUrl": String,
"cfg": path,
Expand Down Expand Up @@ -43,18 +43,20 @@ var knownOpts = {
"uploadedfile": String,

// category
"name":String
"name": String,

"image": String

};

var shortHands = {"-h":["-help"], "url": ["-targetUrl"], "t": ["-token"], "file": ["-uploadedfile"]};
var shortHands = {"-h": ["-help"], "url": ["-targetUrl"], "t": ["-token"], "file": ["-uploadedfile"], "s": ["-src"]};
var parsed = nopt(knownOpts, shortHands, process.argv, 1);
var options = {};


if(parsed.help){
if (parsed.help) {
console.log("" +
"usage: tistory [blog|post|category] [info|list|read|write|update|attach|find] -[options] [values]"
"usage: tistory [blog|post|category|markdown] [info|list|read|write|update|attach|find|image|rewrite] -[options] [values]"
);
process.exit();
}
Expand Down Expand Up @@ -218,19 +220,19 @@ else if (command === "post") {
});
}
}
else if (command === 'category'){
else if (command === 'category') {
var params = {};

if (subCommand === 'list'){
tistory.category.list(function(err,body){
out(body);
end();
});
if (subCommand === 'list') {
tistory.category.list(function (err, body) {
out(body);
end();
});
}
else if (subCommand === 'find'){
if(parsed.name){
params = util._extend(params, {name:parsed.name});
tistory.category.find(params, function(err, body){
else if (subCommand === 'find') {
if (parsed.name) {
params = util._extend(params, {name: parsed.name});
tistory.category.find(params, function (err, body) {
out(body);
end();
});
Expand All @@ -239,4 +241,24 @@ else if (command === 'category'){
}
}
}
else if (command === 'markdown') {
var params = {};

if (subCommand == 'rewrite') {
if (parsed.image === 'attached') {

if (!fs.existsSync(parsed.src)) {
logger.warn('Not found file : ' + parsed.src);

} else {
params = util._extend(params, {src: parsed.src, type: 'markdown'});
}

tistory.replaceImageToAttached(params, function (err, body) {
out(body);
end();
});

}
}
}
28 changes: 17 additions & 11 deletions lib/tistory/tistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function Tistory(options) {
//TODO: 현재 markdown 파일만 가능한데 HTML 파일도 변환 가능하게 수정할 것
Tistory.prototype.replaceImageToAttached = function (params, callback) {
var src = params.src;
var dest = path.join(path.dirname(params.src), path.basename(params.src, path.extname(params.src))+".copy"+ path.extname(params.src));
var dest = path.join(path.dirname(params.src), path.basename(params.src, path.extname(params.src)) + ".copy" + path.extname(params.src));
var self = this;

fs.readFile(src, function (err, data) {
Expand All @@ -63,24 +63,30 @@ Tistory.prototype.replaceImageToAttached = function (params, callback) {
function replacer(whole, a, href, c, offset, input, done) {
var replacedHref;

self.post.attach({uploadedfile: href}, function (err, body) {
if (err) {
logger.error(err);
} else {
var json = JSON.parse(body);
replacedHref = json.tistory.url;
done(null, a + replacedHref + c);
}
});
if ((href.indexOf('http://') == -1) && (href.indexOf('https://') == -1) && (href.indexOf("data:") == -1)) {
self.post.attach({uploadedfile: href}, function (err, body) {
if (err) {
logger.error(err);
} else {
var json = JSON.parse(body);
replacedHref = json.tistory.url;
done(null, a + replacedHref + c);
}
});
} else {
done(null, a + href + c);
}
}


asyncReplace(body, /(!\[.*?\]\()(.+?)(\))/g, replacer, function (err, result) {
fs.writeFile(dest, result, function(err){
fs.writeFile(dest, result, function (err) {
return callback(err, result);
});

});


});

};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-tistory",
"version": "0.1.0",
"version": "0.1.1",
"description": "Tistory Client module",
"main": "lib/tistory",
"bin":{
Expand Down
2 changes: 1 addition & 1 deletion test/tistory-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Tistory', function () {
describe.only('replace local image path to attached remote file url', function () {
it('replace file path to attached file in document', function (done) {
var params = {
src:'/Users/saltfactory/Dropbox/Blog/posts/2014-07-16-using-passport-tistory-in-express.md',
src:'/Users/saltfactory/Dropbox/Blog/posts/2014-08-06-upgrade-latest-docker-using-with-homebrew.md',
type:'markdown'
};

Expand Down

0 comments on commit f6618ed

Please sign in to comment.