Skip to content

Commit bc08dee

Browse files
committed
code reorg to emulate practices from learning site import
1 parent 0a2ffd1 commit bc08dee

File tree

7 files changed

+392
-241
lines changed

7 files changed

+392
-241
lines changed

nanoc2wordpress/config-sample.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"db_name" : "yourWpDbName",
55
"db_user" : "yourDbUser",
66
"db_password" : "yourDbPassword",
7-
"site_id" : 3
7+
"db_port" : 3306,
8+
"site_id" : null
89
}

nanoc2wordpress/main.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env node
2+
3+
var Step = require("step"),
4+
exec = require( "child_process" ).exec,
5+
UserError = require("./user-error"),
6+
wordpress = require("./wordpress"),
7+
nanoc = require("./nanoc"),
8+
config = require("./config"),
9+
options = require("optimist").default({ compile: true, pull: true }).argv,
10+
site;
11+
12+
process.on( "uncaughtException", function( error ) {
13+
// TODO: log error to file
14+
console.error( "uncaught exception" );
15+
console.error( error );
16+
console.error( error.stack );
17+
process.exit();
18+
});
19+
20+
function gitPull( continuation ) {
21+
console.log( "Doing 'git pull' in '" + config.git_dir + "'" );
22+
exec( "git pull", { cwd: config.config.git_dir }, function( error, stdout, stderr ) {
23+
console.log(error, stdout, stderr)
24+
if ( error !== null ) {
25+
console.error( error );
26+
process.exit( error.code );
27+
}
28+
continuation();
29+
});
30+
}
31+
32+
function processSite( finish ) {
33+
Step(
34+
// Step 1: Optionally do a git pull
35+
function() {
36+
options.pull ? gitPull( this ) : this();
37+
},
38+
// Step 2: Optionally compile the site with nanoc
39+
function() {
40+
options.compile ? nanoc.compile( this ) : this();
41+
},
42+
// Step 3: Process the nanoc output into a data structure
43+
function() {
44+
nanoc.process( this );
45+
},
46+
// Step 4: Save processed data and truncate wordpress tables
47+
function( err, data ) {
48+
site = data;
49+
console.log( "Truncating wordpress tables" )
50+
wordpress.reset( this );
51+
},
52+
// Step 5: Create Wordpress Taxonomy
53+
function() {
54+
console.log( "Creating WordPress Taxonomy" )
55+
wordpress.createTerms( site.categories, this )
56+
},
57+
// Step 6: Create articles in WordPress
58+
function(){
59+
console.log( "Creating WordPress Pages" );
60+
wordpress.createPages( site.articles, this )
61+
},
62+
// Step 7: Flush WordPress rewrite rules
63+
function(){
64+
console.log( "Flushing WordPress rewrites" )
65+
wordpress.flush( this )
66+
},
67+
function(){
68+
finish();
69+
})
70+
}
71+
72+
processSite(function(error, data) {
73+
wordpress.end()
74+
console.log("All done!");
75+
// TODO: log error to file
76+
if (error) {
77+
console.log(error, error.stack);
78+
}
79+
process.exit();
80+
});
81+

nanoc2wordpress/nanoc.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
var Step = require("step"),
2+
cp = require( "child_process" )
3+
exec = cp.exec,
4+
spawn = cp.spawn,
5+
fs = require("fs"),
6+
jsdom = require( "jsdom" ),
7+
_ = require( "underscore" ),
8+
config = require("./config"),
9+
10+
OUTPUT_DIR = config.git_dir + "/output",
11+
META_REGEX = /<script id="nanoc_meta".*<\/script>(\\n)*/,
12+
13+
site = {
14+
articles: [],
15+
categories: []
16+
};
17+
18+
_.mixin(require('underscore.string').exports());
19+
20+
function compile( continuation ) {
21+
console.log( "Doing 'nanoc compile' in '" + config.git_dir + "'" );
22+
exec( "nanoc compile", { cwd: config.git_dir }, function( error, stdout, stderr ) {
23+
console.log( stdout );
24+
if ( error !== null ) {
25+
console.error( error );
26+
process.exit( error.code );
27+
}
28+
continuation( error, stdout );
29+
});
30+
}
31+
32+
function findFiles( continuation ) {
33+
var finder = require( "findit" ).find( OUTPUT_DIR );
34+
35+
finder.on( "file", function( file ) {
36+
var locPath = file.replace( OUTPUT_DIR, "" ),
37+
chunkedPath = locPath.split("/"),
38+
isHome = chunkedPath.length == 2,
39+
isCategory = chunkedPath.length == 3;
40+
if ( isHome ) {
41+
// TODO: Deal with home page
42+
} else if ( isCategory ) {
43+
site.categories.push({ path: file })
44+
} else if ( locPath.indexOf("/assets/") !=0 ) {
45+
site.articles.push({ path: file });
46+
}
47+
});
48+
49+
finder.on( "end", continuation );
50+
}
51+
52+
function processCategories( continuation ) {
53+
54+
Step(
55+
function() {
56+
var group = this.group();
57+
site.categories.forEach(function( cat ) {
58+
cat.contents = fs.readFileSync( cat.path, "utf8" );
59+
jsdom.env( cat.contents, group() );
60+
});
61+
},
62+
function ( err, windows ) {
63+
windows.forEach( function(win, index) {
64+
var meta = win.document.getElementById('nanoc_meta'),
65+
cat = site.categories[ index ];
66+
if (meta) {
67+
_.extend( cat, JSON.parse(meta.textContent) )
68+
cat.contents = _.trim(cat.contents.replace( META_REGEX, ""));
69+
}
70+
});
71+
continuation();
72+
});
73+
}
74+
75+
function processArticles( continuation ) {
76+
Step(
77+
function readFiles() {
78+
var group = this.group();
79+
site.articles.forEach(function( file ) {
80+
var filename_ext = file.path.split( "/" ).slice( -1 )[ 0 ];
81+
82+
file.filename = file.path.replace( OUTPUT_DIR, "" ),
83+
file.contents = fs.readFileSync( file.path, "utf8" );
84+
85+
jsdom.env( file.contents, group() );
86+
});
87+
},
88+
function processMeta(err, windows) {
89+
var ts = new Date();
90+
windows.forEach( function(win, index) {
91+
var meta = win.document.getElementById('nanoc_meta'),
92+
file = site.articles[ index ];
93+
if (meta) {
94+
_.extend( file, JSON.parse(meta.textContent) )
95+
file.contents = _.trim(file.contents.replace( META_REGEX, ""));
96+
file.slug = file.filename.replace( "/"+file.chapter+ "/", "").replace("/index.html", "");
97+
file.date = ts;
98+
}
99+
});
100+
continuation();
101+
});
102+
}
103+
104+
var nanoc = module.exports = {
105+
compile: function( continuation ) {
106+
console.log( "Doing 'nanoc compile' in '" + config.git_dir + "'" );
107+
exec( "nanoc compile", { cwd: config.git_dir }, function( error, stdout, stderr ) {
108+
console.log( stdout );
109+
if ( error !== null ) {
110+
console.error( error );
111+
process.exit( error.code );
112+
}
113+
continuation( error, stdout );
114+
});
115+
},
116+
process: function( continuation ) {
117+
Step(
118+
function() {
119+
findFiles( this );
120+
},
121+
function() {
122+
processCategories( this );
123+
},
124+
function() {
125+
processArticles( this );
126+
},
127+
function() {
128+
continuation( null, site );
129+
})
130+
}
131+
};

0 commit comments

Comments
 (0)