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

Commit 222ec24

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 d4f9219 commit 222ec24

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
@@ -65,8 +65,9 @@ mixin includeShared(filePath, region)
6565
!=partial(newPath)
6666

6767
mixin makeExample(_filePath, region, _title, stylePatterns)
68-
- var filePath = adjustTsExamplePath4Dart ? adjustTsExamplePath4Dart(_filePath) : _filePath;
69-
- var title = adjustTsExampleTitle4Dart ? adjustTsExampleTitle4Dart(_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`.
@@ -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 proj 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(adjustTsExamplePath4Dart) ex.filePath = adjustTsExamplePath4Dart(ex.filePath);
231+
- if(adjustTsExampleTitle4Dart) ex.title = adjustTsExampleTitle4Dart(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/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
@@ -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)