Skip to content

Commit 05e8b34

Browse files
Publish standalone Document Parser (ampproject#1552)
Make it possible to standalone parse a document. Also set different runtime types.
1 parent 5444475 commit 05e8b34

8 files changed

+106
-7
lines changed

lib/Document.js

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ module.exports = class Document {
3333
this.body = '';
3434
this.elementsAfterBody = '';
3535
this.isAmpStory = false;
36+
this.isAmpWeb = false;
37+
this.isAmpAds = false;
38+
this.isAmpEmail = false;
3639
}
3740

3841
addSection(section) {

lib/DocumentParser.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ class DocumentParser {
140140
updateHtmlTag(line) {
141141
if (this.extractTag(line) == 'html') {
142142
this.document.lang = this.attr(line, 'lang') || DEFAULT_LANG;
143-
return;
143+
if (line.includes('4ads')) {
144+
this.document.isAmpAds = true;
145+
} else if (line.includes('4email')) {
146+
this.document.isAmpEmail = true;
147+
} else {
148+
this.document.isAmpWeb = true;
149+
}
144150
}
145151
}
146152

@@ -159,7 +165,7 @@ class DocumentParser {
159165
if (!this.document.title) {
160166
const title = /\s*<title>(.*?)<\/title>/g.exec(line);
161167
if (title) {
162-
this.document.title = title[1];
168+
this.document.title = title[1].trim();
163169
}
164170
}
165171
if (line.indexOf('<style amp-custom>') > -1) {

lib/ExampleFile.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class ExampleFile {
136136
}
137137

138138
title() {
139-
return FileName.toString(this.fileName());
139+
return FileName.toString(this.fileName()).trim();
140140
}
141141

142142
url() {

lib/Preview.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,4 @@ function writeFile(filePath, content) {
177177
fs.writeFileSync(filePath, content);
178178
};
179179

180-
module.exports = {};
181-
module.exports.generatePreview = generateEmbeds;
180+
module.exports = generateEmbeds;

lib/Standalone.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
"use strict";
18+
19+
const {parse} = require('./DocumentParser.js');
20+
const ExampleFile = require('./ExampleFile.js');
21+
22+
const {promisify} = require('util');
23+
const fs = require('fs');
24+
const readFileAsync = promisify(fs.readFile);
25+
26+
module.exports = async (filePath) => {
27+
const contents = await readFileAsync(filePath, 'utf-8');
28+
const doc = parse(contents);
29+
const exampleFile = ExampleFile.fromPath(filePath);
30+
if (!doc.title) {
31+
console.warn(`${filePath} has no title`);
32+
doc.title = exampleFile.title();
33+
} else if (doc.title !== exampleFile.title()) {
34+
console.warn(`${filePath} has invalid title: "${exampleFile.title()}" vs "${doc.title}"`);
35+
}
36+
exampleFile.document = doc;
37+
return exampleFile;
38+
}

lib/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Copyright 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS-IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
"use strict";
18+
19+
module.exports = {};
20+
module.exports.generatePreview = require('./Preview.js');
21+
module.exports.loadExample = require('./Standalone.js');
22+

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
22
"name": "amp-by-example",
3-
"version": "0.1.5",
3+
"version": "0.2.1",
44
"description": "AMP by example",
55
"repository": {
66
"type": "git",
77
"url": "https://github.com/ampproject/amp-by-example.git"
88
},
9-
"main": "lib/Preview.js",
9+
"main": "lib/index.js",
1010
"engines": {
1111
"node": ">=4.0.0"
1212
},

spec/compiler/DocumentParserSpec.js

+31
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,37 @@ describe("DocumentParser", function() {
256256
});
257257
});
258258

259+
describe('parses runtime', function() {
260+
it('amp-story', function() {
261+
const document = parse('<html ⚡>', '<body>', '<amp-story standalone>', '</amp-story>', '</body>');
262+
expect(document.isAmpStory).toBe(true);
263+
expect(document.isAmpWeb).toBe(true);
264+
expect(document.isAmpEmail).toBe(false);
265+
expect(document.isAmpAds).toBe(false);
266+
});
267+
it('amp-mail', function() {
268+
const document = parse('<html ⚡4email>', '<body>', '</body>');
269+
expect(document.isAmpStory).toBe(false);
270+
expect(document.isAmpWeb).toBe(false);
271+
expect(document.isAmpEmail).toBe(true);
272+
expect(document.isAmpAds).toBe(false);
273+
});
274+
it('amp-ad', function() {
275+
const document = parse('<html ⚡4ads>', '<body>', '</body>');
276+
expect(document.isAmpStory).toBe(false);
277+
expect(document.isAmpWeb).toBe(false);
278+
expect(document.isAmpEmail).toBe(false);
279+
expect(document.isAmpAds).toBe(true);
280+
});
281+
it('amp-web', function() {
282+
const document = parse('<html ⚡>', '<body>', '</body>');
283+
expect(document.isAmpStory).toBe(false);
284+
expect(document.isAmpWeb).toBe(true);
285+
expect(document.isAmpEmail).toBe(false);
286+
expect(document.isAmpAds).toBe(false);
287+
});
288+
});
289+
259290
function newSection(comment, doc, preview, isFirstSection, isLastSection) {
260291
const section = new CodeSection(comment, doc, preview);
261292
section.isLastSection = isLastSection;

0 commit comments

Comments
 (0)