Skip to content

Commit b48cde3

Browse files
chalinthso
authored andcommitted
chore(_util-fns): generalize makeExample, drop makeProjExample (angular#1510)
+ Rename adjustExample{Path|Title} to adjustTsExample*4Dart + `makeExample` can now work with both doc folder relative and project relative paths.
1 parent 41947cb commit b48cde3

File tree

4 files changed

+68
-44
lines changed

4 files changed

+68
-44
lines changed

public/_includes/_util-fns.jade

+60-36
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ mixin ifDocsFor(langPattern)
5353
block
5454

5555
//- Use to map inlined (prose) TS paths into, say, Dart paths via the
56-
//- adjustExamplePath transformer function.
56+
//- adjustTsExamplePathForDart transformer function.
5757
mixin adjExPath(path)
58-
if adjustExamplePath
59-
| #{adjustExamplePath(path)}
58+
if adjustTsExamplePathForDart
59+
| #{adjustTsExamplePathForDart(path)}
6060
else
6161
| #{path}
6262

@@ -65,8 +65,9 @@ mixin includeShared(filePath, region)
6565
!=partial(newPath)
6666

6767
mixin makeExample(_filePath, region, _title, stylePatterns)
68-
- var filePath = adjustExamplePath ? adjustExamplePath(_filePath) : _filePath;
69-
- var title = adjustExampleTitle ? adjustExampleTitle(_title) : _title;
68+
- var adjustments = adjustExamplePathAndTitle({filePath:_filePath, title:_title});
69+
- var filePath = adjustments.filePath;
70+
- var title = adjustments.title;
7071
- var language = attributes.language || getExtn(filePath);
7172
- var frag = getFrag(filePath, region);
7273
- var defaultFormat = frag.split('\n').length > 2 ? "linenums" : "";
@@ -82,35 +83,21 @@ mixin makeExample(_filePath, region, _title, stylePatterns)
8283
code-example(language="#{language}" format="#{format}")
8384
!= styleString(frag, stylePatterns)
8485

85-
//- Like makeExample, but the first argument is a path that is
86-
//- relative to the project root. Unless title is defined,
87-
//- the project relative path will be used.
88-
mixin makeProjExample(projRootRelativePath, region, title, stylePatterns)
89-
- var relPath = projRootRelativePath.trim();
90-
- var filePath = getExampleName() + '/ts/' + relPath;
91-
- if (!title) {
92-
- // Is path like styles.1.css? Then drop the '.1' qualifier:
93-
- var matches = relPath.match(/^(.*)\.\d(\.\w+)$/);
94-
- title = matches ? matches[1] + matches[2] : relPath;
95-
- }
96-
+makeExample(filePath, region, title, stylePatterns)
97-
98-
//- Like makeExample, but doesn't show line numbers, and the first
99-
//- argument is a path that is relative to the example project root.
100-
//- Unless title is defined, the project relative path will be used.
101-
//- Title will always end with a phrase in parentheses; if no such
102-
//- ending is given, then the title will be suffixed with
103-
//- either "(excerpt)", or "(#{region})" when region is defined.
104-
mixin makeExcerpt(projRootRelativePath, region, title, stylePatterns)
105-
- var relPath = projRootRelativePath.trim();
106-
- var filePath = getExampleName() + '/ts/' + relPath;
107-
- if (!title) {
108-
- // Is path like styles.1.css? Then drop the '.1' qualifier:
109-
- var matches = relPath.match(/^(.*)\.\d(\.\w+)$/);
110-
- title = matches ? matches[1] + matches[2] : relPath;
111-
- }
112-
- var excerpt = region || 'excerpt';
113-
- if (title && !title.match(/\([\w ]+\)$/)) title = title + ' (' + excerpt + ')';
86+
//- Like makeExample, but: (1) doesn't show line numbers. (2) If region
87+
//- is omitted and title is 'foo (r)' then region is taken as 'r'.
88+
//- (3) Title will always end with a phrase in parentheses; if no such
89+
//- ending is given or is just (), then the title will be suffixed with
90+
//- either "(excerpt)", or "(#{_region})" when _region is defined.
91+
mixin makeExcerpt(_filePath, _region, _title, stylePatterns)
92+
- var matches = _filePath.match(/(.*)\s+\(([\w ]*)\)$/);
93+
- var parenText;
94+
- if (matches) { _filePath = matches[1]; parenText = matches[2]; }
95+
- var adjustments = adjustExamplePathAndTitle({filePath:_filePath, title:_title});
96+
- var filePath = adjustments.filePath;
97+
- var title = adjustments.title;
98+
- var region = _region || parenText;
99+
- var excerpt = !region || parenText === '' ? 'excerpt' : region;
100+
- if (title) title = title + ' (' + excerpt + ')';
114101
+makeExample(filePath, region, title, stylePatterns)(format='.')
115102

116103
//- Extract the doc example name from `current`.
@@ -121,10 +108,10 @@ mixin makeExcerpt(projRootRelativePath, region, title, stylePatterns)
121108

122109
mixin makeTabs(filePaths, regions, tabNames, stylePatterns)
123110
- filePaths = strSplit(filePaths);
124-
- if (adjustExamplePath) filePaths = filePaths.map(adjustExamplePath);
111+
- if (adjustTsExamplePathForDart) filePaths = filePaths.map(adjustTsExamplePathForDart);
125112
- regions = strSplit(regions, filePaths.length);
126113
- tabNames = strSplit(tabNames, filePaths.length);
127-
- if (adjustExampleTitle) tabNames = tabNames.map(adjustExampleTitle);
114+
- if (adjustTsExampleTitleForDart) tabNames = tabNames.map(adjustTsExampleTitleForDart);
128115

129116
code-tabs
130117
each filePath,index in filePaths
@@ -216,6 +203,43 @@ script.
216203
return CCSstyle[style] = value
217204
}
218205
//---------------------------------------------------------------------------------------------------------
206+
//- Converts the given project-relative path (like 'app/main.ts')
207+
//- to a doc folder relative path (like 'quickstart/ts/app/main.ts')
208+
//- by prefixing it with '<example-name>/ts/'. If title is not given,
209+
//- then the project-relative path is used, adjusted to remove numeric
210+
//- file version qualifiers; e.g. 'styles.1.css' becomes 'styles.css'.
211+
- var adjExampleProjPathAndTitle = function(ex/*:{filePath,title}*/) {
212+
- // E.g. of a project relative path is 'app/main.ts'
213+
- if (ex.title === null || ex.title === undefined) {
214+
- // Title is not given so take it to be ex.filePath.
215+
- // Is title like styles.1.css? Then drop the '.1' qualifier:
216+
- var matches = ex.filePath.match(/^(.*)\.\d(\.\w+)$/);
217+
- ex.title = matches ? matches[1] + matches[2] : ex.filePath;
218+
- }
219+
- ex.filePath = getExampleName() + '/' + _docsFor + '/' + ex.filePath;
220+
- return ex;
221+
- };
222+
223+
//- If the given path is project relative, then first convert it using
224+
//- adjExampleProjPathAndTitle(ex). Then the path is adjusted to match
225+
//- the documentation language.
226+
- var adjustExamplePathAndTitle = function(ex/*:{filePath,title}*/) {
227+
- // Not a doc folder relative path? Assume that it is app project relative.
228+
- if(isProjRelDir(ex.filePath)) adjExampleProjPathAndTitle(ex);
229+
- // Adjust doc folder relative paths if adjustment functions exist.
230+
- if(adjustTsExamplePathForDart) ex.filePath = adjustTsExamplePathForDart(ex.filePath);
231+
- if(adjustTsExampleTitleForDart) ex.title = adjustTsExampleTitleForDart(ex.title);
232+
- return ex;
233+
- };
234+
235+
//- Returns truthy iff path is example project relative.
236+
- var isProjRelDir = function(path) {
237+
- return !path.match(/\/(js|ts|dart)(-snippets)?\//) && !path.endsWith('e2e-spec.js');
238+
- // Last conjunct handles case for shared project e2e test file like
239+
- // cb-component-communication/e2e-spec.js (is shared between ts & dart)
240+
- // TODO: generalize: compare start with getExampleName(); which needs to be fixed.
241+
- };
242+
219243
- var translatePath = function(filePath, region) {
220244
- filePath = filePath.trim();
221245
- var regionPad = (region && region.length) ? '-' + region.toString() : '';

public/docs/dart/latest/_util-fns.jade

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ mixin liveExampleLink2(linkText, exampleUrlPartName)
3232
- var href = 'http://github.com/angular-examples/' + ex;
3333
span #[+liveExampleLink(linkText, ex)] (#[a(href='#{href}' target="_blank") #{srcText}])
3434

35-
- var adjustExamplePath = function(_path) {
35+
- var adjustTsExamplePathForDart = function(_path) {
3636
- if(!_path) return _path;
3737
- var path = _path.trim();
3838
- var folder = getFolder(path);
@@ -52,15 +52,15 @@ mixin liveExampleLink2(linkText, exampleUrlPartName)
5252
- return (folder ? folder + '/' : '') + baseNameNoExt + (extn ? '.' + extn : '');
5353
- };
5454

55-
- var adjustExampleTitle = function(_title) {
56-
- if(!_title || !adjustExamplePath) return _title;
55+
- var adjustTsExampleTitleForDart = function(_title) {
56+
- if(!_title || !adjustTsExamplePathForDart) return _title;
5757
- var title = _title.trim();
5858
- // Assume title is a path if it ends with an extension like '.foo',
5959
- // optionally followed by some comment in parentheses.
6060
- var matches = title.match(/(.*\.\w+)($|\s*\([\w ]+\)$)/);
6161
- if(matches && matches.length == 3) {
6262
- // e.g. matches == ['abc.ts (excerpt)', 'abc.ts', ' (excerpt)']
63-
- var path = adjustExamplePath(matches[1]);
63+
- var path = adjustTsExamplePathForDart(matches[1]);
6464
- title = path + matches[2];
6565
- }
6666
- return title;

public/docs/dart/latest/guide/server-communication.jade

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ block http-providers
3535
[ng2dtri]: https://github.com/angular/angular/wiki/Angular-2-Dart-Transformer#resolved_identifiers
3636

3737
- var stylePattern = { pnk: /(resolved_identifiers:|Browser.*)/gm, otl: /(- angular2:)|(transformers:)/g };
38-
+makeExcerpt('pubspec.yaml', 'transformers', 'pubspec.yaml (transformers)', stylePattern)
38+
+makeExcerpt('pubspec.yaml', 'transformers', null, stylePattern)
3939

4040
block getheroes-and-addhero
4141
:marked

public/docs/ts/latest/quickstart.jade

+3-3
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ p.
211211
#[b Create the component file]
212212
#[code #[+adjExPath('app/app.component.ts')]] (in this newly created directory) with the following content:
213213

214-
+makeProjExample('app/app.component.ts')
214+
+makeExample('app/app.component.ts')
215215

216216
.l-verbose-section
217217
:marked
@@ -300,7 +300,7 @@ block create-main
300300
Now we need something to tell Angular to load the root component.
301301
Create the file #[code #[+adjExPath('app/main.ts')]] with the following content:
302302

303-
+makeProjExample('app/main.ts')
303+
+makeExample('app/main.ts')
304304

305305
.l-verbose-section
306306
:marked
@@ -345,7 +345,7 @@ h2#index Step 4: Add #[code index.html]
345345
In the *#{_indexHtmlDir}* folder
346346
create an `index.html` file and paste the following lines into it:
347347

348-
+makeProjExample('index.html')
348+
+makeExample('index.html')
349349

350350
.l-verbose-section
351351
:marked

0 commit comments

Comments
 (0)