2015-03-23 17:55:49 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
2015-02-12 04:26:43 +00:00
|
|
|
var fs = require('fs')
|
|
|
|
var glob = require('glob');
|
|
|
|
var mkdirp = require('mkdirp');
|
|
|
|
var optimist = require('optimist');
|
|
|
|
var path = require('path');
|
2015-02-12 22:43:41 +00:00
|
|
|
var extractDocs = require('./extractDocs');
|
2015-02-12 04:26:43 +00:00
|
|
|
var argv = optimist.argv;
|
|
|
|
|
|
|
|
function splitHeader(content) {
|
|
|
|
var lines = content.split('\n');
|
|
|
|
for (var i = 1; i < lines.length - 1; ++i) {
|
|
|
|
if (lines[i] === '---') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {
|
2015-02-19 00:39:28 +00:00
|
|
|
header: i < lines.length - 1 ?
|
|
|
|
lines.slice(1, i + 1).join('\n') : null,
|
2015-02-12 04:26:43 +00:00
|
|
|
content: lines.slice(i + 1).join('\n')
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function backtickify(str) {
|
2015-04-08 18:06:56 +00:00
|
|
|
var escaped = '`' + str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/{/g, '\\{') + '`';
|
2015-02-12 04:26:43 +00:00
|
|
|
// Replace require( with require\( so node-haste doesn't replace example
|
|
|
|
// require calls in the docs
|
|
|
|
return escaped.replace(/require\(/g, 'require\\(');
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute() {
|
|
|
|
var MD_DIR = '../docs/';
|
|
|
|
|
2015-03-05 02:10:12 +00:00
|
|
|
var files = glob.sync('src/react-native/docs/*.*');
|
2015-02-12 22:43:41 +00:00
|
|
|
files.forEach(function(file) {
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(file);
|
|
|
|
} catch(e) {
|
|
|
|
/* seriously, unlink throws when the file doesn't exist :( */
|
|
|
|
}
|
2015-02-12 04:26:43 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
var metadatas = {
|
|
|
|
files: [],
|
|
|
|
};
|
|
|
|
|
2015-02-12 22:43:41 +00:00
|
|
|
function handleMarkdown(content) {
|
|
|
|
var metadata = {};
|
|
|
|
|
|
|
|
// Extract markdown metadata header
|
|
|
|
var both = splitHeader(content);
|
2015-02-19 00:39:28 +00:00
|
|
|
if (!both.header) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-12 22:43:41 +00:00
|
|
|
var lines = both.header.split('\n');
|
|
|
|
for (var i = 0; i < lines.length - 1; ++i) {
|
|
|
|
var keyvalue = lines[i].split(':');
|
|
|
|
var key = keyvalue[0].trim();
|
|
|
|
var value = keyvalue.slice(1).join(':').trim();
|
|
|
|
// Handle the case where you have "Community #10"
|
|
|
|
try { value = JSON.parse(value); } catch(e) { }
|
|
|
|
metadata[key] = value;
|
|
|
|
}
|
2015-05-07 23:05:30 +00:00
|
|
|
|
|
|
|
if (metadata.sidebar !== false) {
|
|
|
|
metadatas.files.push(metadata);
|
|
|
|
}
|
2015-02-12 22:43:41 +00:00
|
|
|
|
|
|
|
if (metadata.permalink.match(/^https?:/)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a dummy .js version that just calls the associated layout
|
|
|
|
var layout = metadata.layout[0].toUpperCase() + metadata.layout.substr(1) + 'Layout';
|
|
|
|
|
|
|
|
var content = (
|
2015-02-12 04:26:43 +00:00
|
|
|
'/**\n' +
|
|
|
|
' * @generated\n' +
|
2015-02-12 22:43:41 +00:00
|
|
|
' * @jsx React.DOM\n' +
|
2015-02-12 04:26:43 +00:00
|
|
|
' */\n' +
|
2015-02-12 22:43:41 +00:00
|
|
|
'var React = require("React");\n' +
|
|
|
|
'var layout = require("' + layout + '");\n' +
|
|
|
|
'var content = ' + backtickify(both.content) + '\n' +
|
|
|
|
'var Post = React.createClass({\n' +
|
|
|
|
' render: function() {\n' +
|
|
|
|
' return layout({metadata: ' + JSON.stringify(metadata) + '}, content);\n' +
|
|
|
|
' }\n' +
|
|
|
|
'});\n' +
|
|
|
|
// TODO: Use React statics after upgrading React
|
|
|
|
'Post.content = content;\n' +
|
|
|
|
'module.exports = Post;\n'
|
2015-02-12 04:26:43 +00:00
|
|
|
);
|
2015-02-12 22:43:41 +00:00
|
|
|
|
|
|
|
var targetFile = 'src/react-native/' + metadata.permalink.replace(/\.html$/, '.js');
|
|
|
|
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
|
|
|
|
fs.writeFileSync(targetFile, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
extractDocs().forEach(handleMarkdown);
|
|
|
|
|
|
|
|
var files = glob.sync(MD_DIR + '**/*.*');
|
|
|
|
files.forEach(function(file) {
|
|
|
|
var extension = path.extname(file);
|
|
|
|
if (extension === '.md' || extension === '.markdown') {
|
|
|
|
var content = fs.readFileSync(file, {encoding: 'utf8'});
|
|
|
|
handleMarkdown(content);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (extension === '.json') {
|
|
|
|
var content = fs.readFileSync(file, {encoding: 'utf8'});
|
|
|
|
metadatas[path.basename(file, '.json')] = JSON.parse(content);
|
|
|
|
}
|
2015-02-12 04:26:43 +00:00
|
|
|
});
|
2015-02-12 22:43:41 +00:00
|
|
|
|
|
|
|
fs.writeFileSync(
|
|
|
|
'core/metadata.js',
|
|
|
|
'/**\n' +
|
|
|
|
' * @generated\n' +
|
|
|
|
' * @providesModule Metadata\n' +
|
|
|
|
' */\n' +
|
|
|
|
'module.exports = ' + JSON.stringify(metadatas, null, 2) + ';'
|
|
|
|
);
|
2015-02-12 04:26:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argv.convert) {
|
2015-02-12 22:43:41 +00:00
|
|
|
console.log('convert!');
|
2015-02-12 04:26:43 +00:00
|
|
|
execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = execute;
|