react-native/website/server/extractDocs.js

284 lines
8.7 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');
var ANDROID_SUFFIX = 'android';
var CROSS_SUFFIX = 'cross';
var IOS_SUFFIX = 'ios';
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function getNameFromPath(filepath) {
var ext = null;
while (ext = path.extname(filepath)) {
filepath = path.basename(filepath, ext);
}
2015-05-07 19:16:48 +00:00
2015-03-23 22:22:47 +00:00
if (filepath === 'LayoutPropTypes') {
return 'Flexbox';
2015-05-07 19:16:48 +00:00
} else if (filepath === 'TransformPropTypes') {
return 'Transforms';
} else if (filepath === 'TabBarItemIOS') {
2015-04-21 17:19:36 +00:00
return 'TabBarIOS.Item';
} else if (filepath === 'AnimatedImplementation') {
return 'Animated';
2015-03-23 22:22:47 +00:00
}
return filepath;
}
function getPlatformFromPath(filepath) {
var ext = null;
while (ext = path.extname(filepath)) {
filepath = path.basename(filepath, ext);
}
if (endsWith(filepath, 'Android')) {
return ANDROID_SUFFIX;
} else if (endsWith(filepath, 'IOS')) {
return IOS_SUFFIX;
}
return CROSS_SUFFIX;
}
function getExample(componentName, componentPlatform) {
var path = '../Examples/UIExplorer/' + componentName + 'Example.js';
if (!fs.existsSync(path)) {
path = '../Examples/UIExplorer/' + componentName + 'Example.'+ componentPlatform +'.js';
2015-03-31 21:05:15 +00:00
if (!fs.existsSync(path)) {
return;
}
}
return {
2015-03-31 19:31:37 +00:00
path: path.replace(/^\.\.\//, ''),
content: fs.readFileSync(path).toString(),
};
}
// Determines whether a component should have a link to a runnable example
function isRunnable(componentName) {
if (componentName === 'AlertIOS') {
return true;
}
return false;
}
// Hide a component from the sidebar by making it return false from
// this function
function shouldDisplayInSidebar(componentName) {
if (componentName === 'Transforms') {
return false;
}
return true;
}
function getNextComponent(i) {
var next;
var filepath = all[i];
if (all[i + 1]) {
var nextComponentName = getNameFromPath(all[i + 1]);
if (shouldDisplayInSidebar(nextComponentName)) {
return slugify(nextComponentName);
} else {
return getNextComponent(i + 1);
}
} else {
return 'network';
}
}
2015-03-19 21:05:07 +00:00
function componentsToMarkdown(type, json, filepath, i, styles) {
var componentName = getNameFromPath(filepath);
var componentPlatform = getPlatformFromPath(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;
json.filepath = filepath.replace(/^\.\.\//, '');
json.componentName = componentName;
json.componentPlatform = componentPlatform;
2015-03-19 21:05:07 +00:00
if (styles) {
json.styles = styles;
}
json.example = getExample(componentName, componentPlatform);
// Put Flexbox into the Polyfills category
var category = (type === 'style' ? 'Polyfills' : type + 's');
var next = getNextComponent(i);
var res = [
'---',
'id: ' + slugify(componentName),
'title: ' + componentName,
2015-03-05 02:10:12 +00:00
'layout: autodocs',
'category: ' + category,
'permalink: docs/' + slugify(componentName) + '.html',
'platform: ' + componentPlatform,
'next: ' + next,
'sidebar: ' + shouldDisplayInSidebar(componentName),
'runnable:' + isRunnable(componentName),
'---',
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)
);
2015-03-25 04:13:55 +00:00
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, e);
2015-03-25 04:13:55 +00:00
json = {};
}
return componentsToMarkdown(type, json, filepath, n++);
};
}
function renderStyle(filepath) {
var json = docgen.parse(
fs.readFileSync(filepath),
docgenHelpers.findExportedObject,
[docgen.handlers.propTypeHandler]
);
// Remove deprecated transform props from docs
if (filepath === "../Libraries/StyleSheet/TransformPropTypes.js") {
['rotation', 'scaleX', 'scaleY', 'translateX', 'translateY'].forEach(function(key) {
delete json['props'][key];
});
}
2015-03-25 04:13:55 +00:00
return componentsToMarkdown('style', json, filepath, n++);
}
var components = [
'../Libraries/Components/ActivityIndicatorIOS/ActivityIndicatorIOS.ios.js',
'../Libraries/Components/DatePicker/DatePickerIOS.ios.js',
'../Libraries/Components/DrawerAndroid/DrawerLayoutAndroid.android.js',
2015-03-03 01:31:26 +00:00
'../Libraries/Image/Image.ios.js',
'../Libraries/CustomComponents/ListView/ListView.js',
'../Libraries/Components/MapView/MapView.js',
'../Libraries/Modal/Modal.js',
2015-11-26 05:26:32 +00:00
'../Libraries/CustomComponents/Navigator/Navigator.js',
'../Libraries/Components/Navigation/NavigatorIOS.ios.js',
'../Libraries/Picker/PickerIOS.ios.js',
'../Libraries/Components/ProgressBarAndroid/ProgressBarAndroid.android.js',
'../Libraries/Components/ProgressViewIOS/ProgressViewIOS.ios.js',
'../Libraries/PullToRefresh/PullToRefreshViewAndroid.android.js',
2016-01-09 15:43:02 +00:00
'../Libraries/Components/RefreshControl/RefreshControl.js',
'../Libraries/Components/ScrollView/ScrollView.js',
'../Libraries/Components/SegmentedControlIOS/SegmentedControlIOS.ios.js',
2015-08-11 17:25:39 +00:00
'../Libraries/Components/SliderIOS/SliderIOS.ios.js',
2015-11-18 15:58:21 +00:00
'../Libraries/Components/Switch/Switch.js',
'../Libraries/Components/TabBarIOS/TabBarIOS.ios.js',
2015-04-21 17:19:36 +00:00
'../Libraries/Components/TabBarIOS/TabBarItemIOS.ios.js',
2015-03-03 01:31:26 +00:00
'../Libraries/Text/Text.js',
2015-03-31 16:46:45 +00:00
'../Libraries/Components/TextInput/TextInput.js',
'../Libraries/Components/ToolbarAndroid/ToolbarAndroid.android.js',
'../Libraries/Components/Touchable/TouchableHighlight.js',
'../Libraries/Components/Touchable/TouchableNativeFeedback.android.js',
'../Libraries/Components/Touchable/TouchableOpacity.js',
'../Libraries/Components/Touchable/TouchableWithoutFeedback.js',
'../Libraries/Components/View/View.js',
'../Libraries/Components/ViewPager/ViewPagerAndroid.android.js',
'../Libraries/Components/WebView/WebView.ios.js',
];
var apis = [
2015-06-11 03:41:21 +00:00
'../Libraries/ActionSheetIOS/ActionSheetIOS.js',
2015-12-18 14:15:22 +00:00
'../Libraries/Utilities/Alert.js',
'../Libraries/Utilities/AlertIOS.js',
'../Libraries/Animated/src/AnimatedImplementation.js',
'../Libraries/AppRegistry/AppRegistry.js',
'../Libraries/AppStateIOS/AppStateIOS.ios.js',
'../Libraries/Storage/AsyncStorage.js',
'../Libraries/Utilities/BackAndroid.android.js',
2015-03-12 18:03:32 +00:00
'../Libraries/CameraRoll/CameraRoll.js',
2015-10-09 21:44:36 +00:00
'../Libraries/Utilities/Dimensions.js',
2015-12-15 15:50:40 +00:00
'../Libraries/Components/Intent/IntentAndroid.android.js',
'../Libraries/Interaction/InteractionManager.js',
2015-08-25 18:23:41 +00:00
'../Libraries/LayoutAnimation/LayoutAnimation.js',
'../Libraries/LinkingIOS/LinkingIOS.js',
2015-10-07 17:04:42 +00:00
'../Libraries/ReactIOS/NativeMethodsMixin.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/Components/ToastAndroid/ToastAndroid.android.js',
'../Libraries/Vibration/VibrationIOS.ios.js',
];
2015-03-19 21:05:07 +00:00
var styles = [
'../Libraries/StyleSheet/LayoutPropTypes.js',
2015-05-07 19:16:48 +00:00
'../Libraries/StyleSheet/TransformPropTypes.js',
2015-03-19 21:05:07 +00:00
'../Libraries/Components/View/ViewStylePropTypes.js',
'../Libraries/Text/TextStylePropTypes.js',
'../Libraries/Image/ImageStylePropTypes.js',
];
2015-03-25 04:13:55 +00:00
var polyfills = [
2015-04-24 20:14:24 +00:00
'../Libraries/GeoLocation/Geolocation.js',
2015-03-25 04:13:55 +00:00
];
var all = components
.concat(apis)
2015-05-07 19:16:48 +00:00
.concat(styles.slice(0, 2))
2015-03-25 04:13:55 +00:00
.concat(polyfills);
2015-05-07 19:16:48 +00:00
var styleDocs = styles.slice(2).reduce(function(docs, filepath) {
2015-03-19 21:05:07 +00:00
docs[path.basename(filepath).replace(path.extname(filepath), '')] =
docgen.parse(
fs.readFileSync(filepath),
docgenHelpers.findExportedObject,
[docgen.handlers.propTypeHandler, docgen.handlers.propTypeCompositionHandler]
2015-03-19 21:05:07 +00:00
);
2015-03-19 21:05:07 +00:00
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')),
2015-05-07 19:16:48 +00:00
styles.slice(0, 2).map(renderStyle),
2015-03-25 04:13:55 +00:00
polyfills.map(renderAPI('Polyfill'))
2015-03-12 18:03:32 +00:00
);
};