react-native/website/server/extractDocs.js

164 lines
5.0 KiB
JavaScript
Raw Normal View History

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-03-19 21:05:07 +00:00
var docgen = require('react-docgen');
var docgenHelpers = require('./docgenHelpers');
var fs = require('fs');
var path = require('path');
var slugify = require('../core/slugify');
var jsDocs = require('../jsdocs/jsdocs.js');
function getNameFromPath(filepath) {
var ext = null;
while (ext = path.extname(filepath)) {
filepath = path.basename(filepath, ext);
}
2015-03-23 22:22:47 +00:00
if (filepath === 'LayoutPropTypes') {
return 'Flexbox';
}
return filepath;
}
2015-03-19 21:05:07 +00:00
function componentsToMarkdown(type, json, filepath, i, styles) {
var componentName = getNameFromPath(filepath);
var docFilePath = '../docs/' + componentName + '.md';
if (fs.existsSync(docFilePath)) {
json.fullDescription = fs.readFileSync(docFilePath).toString();
}
2015-03-12 18:03:32 +00:00
json.type = type;
2015-03-19 21:05:07 +00:00
if (styles) {
json.styles = styles;
}
var res = [
'---',
'id: ' + slugify(componentName),
'title: ' + componentName,
2015-03-05 02:10:12 +00:00
'layout: autodocs',
2015-03-23 22:22:47 +00:00
'category: ' + (type === 'style' ? 'Polyfills' : type + 's'),
'permalink: docs/' + slugify(componentName) + '.html',
2015-03-21 17:59:41 +00:00
'next: ' + (all[i + 1] ?
slugify(getNameFromPath(all[i + 1])) :
'network'),
'---',
2015-03-05 02:10:12 +00:00
JSON.stringify(json, null, 2),
].filter(function(line) { return line; }).join('\n');
return res;
}
2015-03-25 04:13:55 +00:00
var n;
function renderComponent(filepath) {
var json = docgen.parse(
fs.readFileSync(filepath),
docgenHelpers.findExportedOrFirst,
docgen.defaultHandlers.concat(docgenHelpers.stylePropTypeHandler)
);
return componentsToMarkdown('component', json, filepath, n++, styleDocs);
}
function renderAPI(type) {
return function(filepath) {
var json;
try {
json = jsDocs(fs.readFileSync(filepath).toString());
} catch(e) {
console.error('Cannot parse file', filepath);
json = {};
}
return componentsToMarkdown(type, json, filepath, n++);
};
}
function renderStyle(filepath) {
var json = docgen.parse(
fs.readFileSync(filepath),
docgenHelpers.findExportedObject,
[docgen.handlers.propTypeHandler]
);
return componentsToMarkdown('style', json, filepath, n++);
}
var components = [
'../Libraries/Components/ActivityIndicatorIOS/ActivityIndicatorIOS.ios.js',
'../Libraries/Components/DatePicker/DatePickerIOS.ios.js',
2015-03-03 01:31:26 +00:00
'../Libraries/Image/Image.ios.js',
'../Libraries/CustomComponents/ListView/ListView.js',
'../Libraries/Components/MapView/MapView.js',
2015-03-26 13:43:45 +00:00
'../Libraries/CustomComponents/Navigator/Navigator.js',
'../Libraries/Components/Navigation/NavigatorIOS.ios.js',
'../Libraries/Picker/PickerIOS.ios.js',
'../Libraries/Components/ScrollView/ScrollView.js',
2015-03-19 20:55:42 +00:00
'../Libraries/Components/SliderIOS/SliderIOS.js',
'../Libraries/Components/SwitchIOS/SwitchIOS.ios.js',
'../Libraries/Components/TabBarIOS/TabBarIOS.ios.js',
2015-03-03 01:31:26 +00:00
'../Libraries/Text/Text.js',
'../Libraries/Components/TextInput/TextInput.ios.js',
'../Libraries/Components/Touchable/TouchableHighlight.js',
'../Libraries/Components/Touchable/TouchableOpacity.js',
'../Libraries/Components/Touchable/TouchableWithoutFeedback.js',
'../Libraries/Components/View/View.js',
'../Libraries/Components/WebView/WebView.ios.js',
];
var apis = [
'../Libraries/Utilities/AlertIOS.js',
2015-03-12 18:03:32 +00:00
'../Libraries/Animation/Animation.js',
'../Libraries/AppRegistry/AppRegistry.js',
'../Libraries/AppStateIOS/AppStateIOS.ios.js',
'../Libraries/Storage/AsyncStorage.ios.js',
2015-03-12 18:03:32 +00:00
'../Libraries/CameraRoll/CameraRoll.js',
'../Libraries/Interaction/InteractionManager.js',
2015-03-12 18:03:32 +00:00
'../Libraries/Animation/LayoutAnimation.js',
'../Libraries/Network/NetInfo.js',
2015-03-25 03:38:29 +00:00
'../Libraries/vendor/react/browser/eventPlugins/PanResponder.js',
2015-03-12 18:03:32 +00:00
'../Libraries/Utilities/PixelRatio.js',
2015-03-25 03:38:29 +00:00
'../Libraries/PushNotificationIOS/PushNotificationIOS.js',
2015-03-12 18:03:32 +00:00
'../Libraries/Components/StatusBar/StatusBarIOS.ios.js',
'../Libraries/StyleSheet/StyleSheet.js',
'../Libraries/Vibration/VibrationIOS.ios.js',
];
2015-03-19 21:05:07 +00:00
var styles = [
'../Libraries/StyleSheet/LayoutPropTypes.js',
'../Libraries/Components/View/ViewStylePropTypes.js',
'../Libraries/Text/TextStylePropTypes.js',
'../Libraries/Image/ImageStylePropTypes.js',
];
2015-03-25 04:13:55 +00:00
var polyfills = [
'../Libraries/GeoLocation/Geolocation.ios.js',
2015-03-25 04:13:55 +00:00
];
var all = components
.concat(apis)
.concat(styles.slice(0, 1))
.concat(polyfills);
2015-03-19 21:05:07 +00:00
var styleDocs = styles.slice(1).reduce(function(docs, filepath) {
docs[path.basename(filepath).replace(path.extname(filepath), '')] =
docgen.parse(
fs.readFileSync(filepath),
docgenHelpers.findExportedObject,
[docgen.handlers.propTypeHandler]
);
return docs;
}, {});
2015-03-12 18:03:32 +00:00
module.exports = function() {
2015-03-25 04:13:55 +00:00
n = 0;
2015-03-12 18:03:32 +00:00
return [].concat(
2015-03-25 04:13:55 +00:00
components.map(renderComponent),
apis.map(renderAPI('api')),
styles.slice(0, 1).map(renderStyle),
polyfills.map(renderAPI('Polyfill'))
2015-03-12 18:03:32 +00:00
);
};