Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 1a29503

Browse files
committed
chore(_util-fns): generalize makeExample, drop makeProjExample
`makeExample` can now work with both doc folder relative and project relative paths.
1 parent 5d1daaa commit 1a29503

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-35
lines changed

public/_includes/_util-fns.jade

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ mixin includeShared(filePath, region)
6060
!=partial(newPath)
6161

6262
mixin makeExample(_filePath, region, _title, stylePatterns)
63-
- var filePath = adjustTsExamplePath4Dart ? adjustTsExamplePath4Dart(_filePath) : _filePath;
64-
- var title = adjustTsExampleTitle4Dart ? adjustTsExampleTitle4Dart(_title) : _title;
63+
- var adjustments = adjustExamplePathAndTitle({filePath:_filePath, title:_title});
64+
- var filePath = adjustments.filePath;
65+
- var title = adjustments.title;
6566
- var language = attributes.language || getExtn(filePath);
6667
- var frag = getFrag(filePath, region);
6768
- var defaultFormat = frag.split('\n').length > 2 ? "linenums" : "";
@@ -77,35 +78,21 @@ mixin makeExample(_filePath, region, _title, stylePatterns)
7778
code-example(language="#{language}" format="#{format}")
7879
!= styleString(frag, stylePatterns)
7980

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

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

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ p.
213213
#[b Create the component file]
214214
#[code #[+adjExPath('app/app.component.ts')]] (in this newly created directory) with the following content:
215215

216-
+makeProjExample('app/app.component.ts')
216+
+makeExample('app/app.component.ts')
217217

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

305-
+makeProjExample('app/main.ts')
305+
+makeExample('app/main.ts')
306306

307307
.l-verbose-section
308308
:marked
@@ -347,7 +347,7 @@ h2#index Step 4: Add #[code index.html]
347347
In the *#{_indexHtmlDir}* folder
348348
create an `index.html` file and paste the following lines into it:
349349

350-
+makeProjExample('index.html')
350+
+makeExample('index.html')
351351

352352
.l-verbose-section
353353
:marked

0 commit comments

Comments
 (0)