-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#804 Update Allowed Tags And Attributes #823
Changes from 17 commits
0b987a6
30064ca
ba459a2
8cff768
dd807a8
a698eea
a066117
cfcf730
8f0a925
a400fa5
cfc0247
7f9a2db
b4bfd81
6f5e39d
2b1443e
c73f92b
f67041d
236968b
eab6207
eef4b8b
ae92a58
4597be5
44554c5
ed3c93d
c3b28c0
4b3ec53
af7b830
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
.DS_Store | ||
vendor |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
diff --git a/validator/validator_gen_md.py b/validator/validator_gen_md.py | ||
new file mode 100644 | ||
index 000000000..80909fd4c | ||
--- /dev/null | ||
+++ b/validator/validator_gen_md.py | ||
@@ -0,0 +1,215 @@ | ||
+# | ||
+# Copyright 2015 The AMP HTML Authors. All Rights Reserved. | ||
+# | ||
+# Licensed under the Apache License, Version 2.0 (the "License"); | ||
+# you may not use this file except in compliance with the License. | ||
+# You may obtain a copy of the License at | ||
+# | ||
+# http://www.apache.org/licenses/LICENSE-2.0 | ||
+# | ||
+# Unless required by applicable law or agreed to in writing, software | ||
+# distributed under the License is distributed on an "AS-IS" BASIS, | ||
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
+# See the License for the specific language governing permissions and | ||
+# limitations under the license. | ||
+# | ||
+"""Generates validator-generated.md""" | ||
+ | ||
+import os | ||
+ | ||
+ | ||
+def GenerateValidatorGeneratedMd(specfile, validator_pb2, text_format, out): | ||
+ """Main method for the markdown generator. | ||
+ | ||
+ This method reads the specfile and emits Markdown to sys.stdout. | ||
+ | ||
+ Args: | ||
+ specfile: Path to validator.protoascii. | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ text_format: The text_format module from the protobuf package, e.g. | ||
+ google.protobuf.text_format. | ||
+ out: a list of lines to output (without the newline characters), to | ||
+ which this function will append. | ||
+ """ | ||
+ out.append('<!-- Generated by %s - do not edit. -->' % | ||
+ os.path.basename(__file__)) | ||
+ out.append( | ||
+ '<!-- WARNING: This does not fully reflect validator.protoascii yet. -->') | ||
+ out.append('') | ||
+ out.append('[Accelerated Mobile Pages Project](https://www.ampproject.org)') | ||
+ out.append('') | ||
+ | ||
+ out.append('# validator.protoascii') | ||
+ out.append('') | ||
+ | ||
+ rules = validator_pb2.ValidatorRules() | ||
+ text_format.Merge(open(specfile).read(), rules) | ||
+ if rules.HasField('spec_file_revision'): | ||
+ out.append('* spec file revision: %d' % rules.spec_file_revision) | ||
+ if rules.HasField('min_validator_revision_required'): | ||
+ out.append('* minimum validator revision required: %d' % | ||
+ rules.min_validator_revision_required) | ||
+ out.append('') | ||
+ out.append('Allowed Tags') | ||
+ out.append('') | ||
+ out.append('[TOC]') | ||
+ out.append('') | ||
+ for (field_desc, field_val) in rules.ListFields(): | ||
+ if field_desc.name == 'tags': | ||
+ for tag_spec in field_val: | ||
+ PrintTagSpec(validator_pb2, tag_spec, out) | ||
+ | ||
+ | ||
+def GetLayout(validator_pb2, layout_index): | ||
+ """Helper function that returns the AmpLayout.Layout name for a given index. | ||
+ | ||
+ See amp.validator.AmpLayout.Layout in validator.proto for details. | ||
+ | ||
+ Args: | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ layout_index: integer representing a particular AmpLayout.Layout | ||
+ Returns: | ||
+ A string which represents the name for this supported layout. | ||
+ """ | ||
+ amp_layout = validator_pb2.DESCRIPTOR.message_types_by_name['AmpLayout'] | ||
+ layouts = amp_layout.fields_by_name['supported_layouts'].enum_type.values | ||
+ return layouts[layout_index].name | ||
+ | ||
+ | ||
+def PrintAmpLayout(validator_pb2, amp_layout, out): | ||
+ """Prints a Markdown version of the given proto message (AmpLayout). | ||
+ | ||
+ See amp.validator.AmpLayout in validator.proto for details of proto message. | ||
+ | ||
+ Args: | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ amp_layout: The AmpLayout message. | ||
+ out: A list of lines to output (without newline characters), to which this | ||
+ function will append. | ||
+ """ | ||
+ for layout in amp_layout.supported_layouts: | ||
+ out.append('* %s' % UnicodeEscape(GetLayout(validator_pb2, layout))) | ||
+ if amp_layout.defines_default_width: | ||
+ out.append('* Defines Default Width') | ||
+ if amp_layout.defines_default_height: | ||
+ out.append('* Defines Default Height') | ||
+ | ||
+ | ||
+def PrintAttrSpec(attr_spec, out): | ||
+ """Prints a Markdown version of the given proto message (AttrSpec). | ||
+ | ||
+ See amp.validator.AttrSpec in validator.proto for details of proto message. | ||
+ | ||
+ Args: | ||
+ attr_spec: The AttrSpec message. | ||
+ out: A list of lines to output (without newline characters), to which this | ||
+ function will append. | ||
+ """ | ||
+ out.append('* %s' % UnicodeEscape(attr_spec.name)) | ||
+ if attr_spec.alternative_names: | ||
+ out.append(' * Alternative Names: %s' % | ||
+ RepeatedFieldToString(attr_spec.alternative_names)) | ||
+ if attr_spec.mandatory: | ||
+ out.append(' * Mandatory') | ||
+ if attr_spec.mandatory_oneof: | ||
+ out.append(' * Mandatory One of: %s' % attr_spec.mandatory_oneof) | ||
+ if attr_spec.value: | ||
+ out.append(' * Required Value: %s' % attr_spec.value) | ||
+ | ||
+ | ||
+def PrintTagSpec(validator_pb2, tag_spec, out): | ||
+ """Prints a Markdown version of the given proto message (TagSpec). | ||
+ | ||
+ See amp.validator.TagSpec in validator.proto for details of proto message. | ||
+ | ||
+ Args: | ||
+ validator_pb2: The proto2 Python module generated from validator.proto. | ||
+ tag_spec: The TagSpec message. | ||
+ out: A list of lines to output (without newline characters), to which this | ||
+ function will append. | ||
+ """ | ||
+ header = '## %s' % UnicodeEscape(tag_spec.tag_name) | ||
+ if tag_spec.spec_name and (tag_spec.tag_name != tag_spec.spec_name): | ||
+ header += ': %s' % UnicodeEscape(tag_spec.spec_name) | ||
+ if tag_spec.deprecation: | ||
+ header += ' (DEPRECATED)' | ||
+ header += '{#%s_%s}' % (UnicodeEscape(tag_spec.tag_name).replace(' ', '_'), | ||
+ UnicodeEscape(tag_spec.spec_name).replace(' ', '_')) | ||
+ out.append('') | ||
+ out.append(header) | ||
+ out.append('') | ||
+ if tag_spec.deprecation: | ||
+ out.append('This is deprecated.') | ||
+ out.append('Please see [%s](%s)' % | ||
+ (UnicodeEscape(tag_spec.deprecation), tag_spec.deprecation_url)) | ||
+ out.append('') | ||
+ if tag_spec.mandatory: | ||
+ out.append('* Mandatory') | ||
+ if tag_spec.mandatory_alternatives: | ||
+ out.append('* Mandatory Alternatives: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_alternatives)) | ||
+ if tag_spec.unique: | ||
+ out.append('* Unique') | ||
+ if tag_spec.mandatory_parent: | ||
+ out.append('* Mandatory Parent: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_parent)) | ||
+ if tag_spec.mandatory_ancestor: | ||
+ out.append('* Mandatory Ancestor: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_ancestor)) | ||
+ if tag_spec.mandatory_ancestor_suggested_alternative: | ||
+ out.append('* Mandatory Ancestor Alternative: %s' % | ||
+ UnicodeEscape(tag_spec.mandatory_ancestor_suggested_alternative)) | ||
+ if tag_spec.disallowed_ancestor: | ||
+ out.append('* Disallowed Ancestor: %s' % | ||
+ RepeatedFieldToString(tag_spec.disallowed_ancestor)) | ||
+ if tag_spec.attrs: | ||
+ out.append('') | ||
+ out.append('Allowed Attributes:') | ||
+ out.append('') | ||
+ for attr_spec in tag_spec.attrs: | ||
+ PrintAttrSpec(attr_spec, out) | ||
+ if (tag_spec.amp_layout.supported_layouts or | ||
+ tag_spec.amp_layout.defines_default_width or | ||
+ tag_spec.amp_layout.defines_default_height): | ||
+ out.append('') | ||
+ out.append('Allowed Layouts:') | ||
+ out.append('') | ||
+ PrintAmpLayout(validator_pb2, tag_spec.amp_layout, out) | ||
+ if tag_spec.spec_url: | ||
+ out.append('') | ||
+ out.append('[Spec](%s)' % tag_spec.spec_url) | ||
+ out.append('') | ||
+ | ||
+ | ||
+def RepeatedFieldToString(field): | ||
+ """Helper function which converts a list into an escaped string. | ||
+ | ||
+ Args: | ||
+ field: A list of strings. | ||
+ Returns: | ||
+ A string, segmented by commas. | ||
+ """ | ||
+ return ', '.join([UnicodeEscape(s) for s in field]) | ||
+ | ||
+ | ||
+def UnderscoreToTitleCase(under_score): | ||
+ """Helper function which converts under_score names to TitleCase. | ||
+ | ||
+ Args: | ||
+ under_score: A name, segmented by under_scores. | ||
+ Returns: | ||
+ A name, segmented as TitleCase. | ||
+ """ | ||
+ segments = under_score.split('_') | ||
+ return ' '.join([s.title() for s in segments]) | ||
+ | ||
+ | ||
+def UnicodeEscape(string): | ||
+ """Helper function which escapes unicode characters. | ||
+ | ||
+ Args: | ||
+ string: A string which may contain unicode characters. | ||
+ Returns: | ||
+ An escaped string. | ||
+ """ | ||
+ return ('' + string).encode('unicode-escape') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
# Go to the right location. | ||
if [[ '.' != $(dirname "$0") ]]; then | ||
cd bin | ||
fi | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I usually do instead of this conditional is:
|
||
|
||
BIN_PATH="$(pwd)" | ||
PROJECT_PATH=$(dirname $PWD) | ||
VENDOR_PATH=$PROJECT_PATH/vendor | ||
|
||
if ! apt-get > /dev/null; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to check if the command exists via: |
||
echo -e "The AMP HTML uses the apt-get, make sure to run this script in a Linux environment" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for |
||
exit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do |
||
fi | ||
|
||
# Install dependencies. | ||
sudo apt-get install git | ||
sudo apt-get install python | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should make if ! command -v git >/dev/null 2>&1; then
echo "Git is required"
exit 1
fi
if ! command -v python >/dev/null 2>&1; then
echo "Python is required"
exit 1
fi There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you are ok with it, I think it is fine to automatically install |
||
sudo apt-get install protobuf-compiler | ||
sudo apt-get install python-protobuf | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can combine all of the dependencies into one call:
|
||
|
||
# Create and go to vendor. | ||
if [[ ! -e $VENDOR_PATH ]]; then | ||
mkdir $VENDOR_PATH | ||
fi | ||
cd $VENDOR_PATH | ||
|
||
# Clone amphtml repo. | ||
if [[ ! -e $VENDOR_PATH/amphtml ]]; then | ||
git clone https://github.com/ampproject/amphtml amphtml | ||
fi | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add an |
||
|
||
# Copy script to location and go there. | ||
cp $BIN_PATH/amphtml-update.py $VENDOR_PATH/amphtml/validator | ||
cd $VENDOR_PATH/amphtml/validator | ||
|
||
# Temporary fix until https://github.com/ampproject/amphtml/issues/12371 is addressed. | ||
if [ ! -f $VENDOR_PATH/amphtml/validator/validator_gen_md.py ]; then | ||
git apply $BIN_PATH/amphtml-fix.diff | ||
fi | ||
|
||
# Run script. | ||
python amphtml-update.py | ||
cp amp_wp/class-amp-allowed-tags-generated.php ../../../includes/sanitizers/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is a draft the bash script to update AMP HTML. This script is meant to run in Linux environment (ex. VVV). To run the script, simply
cd
to the plugin root and runbash bin/amphtml-update.sh
.