You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@annotator.apache.org by ra...@apache.org on 2017/05/08 02:55:31 UTC
incubator-annotator git commit: Implement fragment stringify [Forced
Update!]
Repository: incubator-annotator
Updated Branches:
refs/heads/fragment-stringify e1614b4a2 -> 4bf46fea5 (forced update)
Implement fragment stringify
Project: http://git-wip-us.apache.org/repos/asf/incubator-annotator/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-annotator/commit/4bf46fea
Tree: http://git-wip-us.apache.org/repos/asf/incubator-annotator/tree/4bf46fea
Diff: http://git-wip-us.apache.org/repos/asf/incubator-annotator/diff/4bf46fea
Branch: refs/heads/fragment-stringify
Commit: 4bf46fea5b4a59df04882bb81f6eaaeefc378701
Parents: f7aa499
Author: Randall Leeds <ra...@apache.org>
Authored: Sun May 7 19:52:55 2017 -0700
Committer: Randall Leeds <ra...@apache.org>
Committed: Sun May 7 19:55:23 2017 -0700
----------------------------------------------------------------------
packages/fragment-identifier/fragment.pegjs | 76 ++++++++++++++++++++++++
packages/fragment-identifier/index.js | 40 +++++++++++++
packages/fragment-identifier/package.json | 12 ++++
packages/fragment-parser/fragment.pegjs | 76 ------------------------
packages/fragment-parser/index.js | 18 ------
packages/fragment-parser/package.json | 12 ----
6 files changed, 128 insertions(+), 106 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-annotator/blob/4bf46fea/packages/fragment-identifier/fragment.pegjs
----------------------------------------------------------------------
diff --git a/packages/fragment-identifier/fragment.pegjs b/packages/fragment-identifier/fragment.pegjs
new file mode 100644
index 0000000..a0ad93b
--- /dev/null
+++ b/packages/fragment-identifier/fragment.pegjs
@@ -0,0 +1,76 @@
+/**
+ * Copyright © 2016 World Wide Web Consortium, (MIT, ERCIM, Keio, Beihang).
+ * http://www.w3.org/Consortium/Legal/2015/doc-license
+ */
+
+{
+ function collect() {
+ var ret = {};
+ var len = arguments.length;
+ for (var i=0; i<len; i++) {
+ for (var p in arguments[i]) {
+ if (arguments[i].hasOwnProperty(p)) {
+ ret[p] = arguments[i][p];
+ }
+ }
+ }
+ return ret;
+ }
+}
+
+
+start =
+ top
+
+top
+ = "state" "(" p:params ")" { return { state: p } }
+ / "selector" "(" p:params ")" { return { selector: p } }
+ / "state=" v:value { return { state: v } }
+ / "selector=" v:value { return { selector: v } }
+
+params
+ = k1: key_value_pair k2:("," key_value_pair)*
+ {
+ var f = k1;
+ for( var i = 0; i < k2.length; i++ ) {
+ f = collect(f, k2[i][1])
+ }
+ return f;
+ }
+
+key_value_pair
+ = "refinedBy=selector(" p:params ")"
+ {
+ return { refinedBy: p }
+ }
+ / "refinedBy=state(" p:params ")"
+ {
+ return { refinedBy: p }
+ }
+ / "startSelector=selector(" p:params ")"
+ {
+ return { startSelector: p };
+ }
+ / "endSelector=selector(" p:params ")"
+ {
+ return { endSelector: p };
+ }
+ / key:key "=" value:value
+ {
+ var f = {};
+ var num = Number(value);
+ f[key] = isNaN(num) ? decodeURIComponent(value): num;
+ return f;
+ }
+
+key
+ = atom
+
+value
+ = atom
+
+atom
+ = chars:validchar+ { return chars.join(""); }
+
+validchar
+ = [a-zA-Z0-9\<\>\/\[\]\:%+@.\-!\$\&\;*_]
http://git-wip-us.apache.org/repos/asf/incubator-annotator/blob/4bf46fea/packages/fragment-identifier/index.js
----------------------------------------------------------------------
diff --git a/packages/fragment-identifier/index.js b/packages/fragment-identifier/index.js
new file mode 100644
index 0000000..c408b17
--- /dev/null
+++ b/packages/fragment-identifier/index.js
@@ -0,0 +1,40 @@
+// @flow
+/**
+ * @license
+ * 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.
+ */
+
+import fragment from './fragment.pegjs';
+
+export const { SyntaxError, parse } = fragment;
+
+/**
+ * Convert a Selector or State into a fragment identifier string.
+ * @param {(Selector|State)} resource
+ * @return {string}
+ */
+export function stringify(resource) {
+ const data = Object.keys(resource).map((key) => {
+ let value = resource[key];
+ if (value instanceof Object) value = value.valueOf();
+ if (value instanceof Object) {
+ value = stringify(value);
+ return `${encodeURIComponent(key)}=${value}`;
+ }
+ return [key, value].map(encodeURIComponent).join('=');
+ }).join(',');
+
+ if (/Selector$/.test(resource.type)) return `selector(${data})`;
+ if (/State$/.test(resource.type)) return `state(${data})`;
+ throw new TypeError('Resource must be a Selector or State');
+}
http://git-wip-us.apache.org/repos/asf/incubator-annotator/blob/4bf46fea/packages/fragment-identifier/package.json
----------------------------------------------------------------------
diff --git a/packages/fragment-identifier/package.json b/packages/fragment-identifier/package.json
new file mode 100644
index 0000000..7b9a2d9
--- /dev/null
+++ b/packages/fragment-identifier/package.json
@@ -0,0 +1,12 @@
+{
+ "name": "@annotator/fragment-identifier",
+ "description": "Convert to and from Web Annotation fragment identifiers.",
+ "author": "Apache Software Foundation",
+ "license": "Apache-2.0",
+ "homepage": "https://annotator.apache.org",
+ "repository": {
+ "type": "git",
+ "url": "https://git-wip-us.apache.org/repos/asf/incubator-annotator.git"
+ },
+ "version": "0.0.0"
+}
http://git-wip-us.apache.org/repos/asf/incubator-annotator/blob/4bf46fea/packages/fragment-parser/fragment.pegjs
----------------------------------------------------------------------
diff --git a/packages/fragment-parser/fragment.pegjs b/packages/fragment-parser/fragment.pegjs
deleted file mode 100644
index a0ad93b..0000000
--- a/packages/fragment-parser/fragment.pegjs
+++ /dev/null
@@ -1,76 +0,0 @@
-/**
- * Copyright © 2016 World Wide Web Consortium, (MIT, ERCIM, Keio, Beihang).
- * http://www.w3.org/Consortium/Legal/2015/doc-license
- */
-
-{
- function collect() {
- var ret = {};
- var len = arguments.length;
- for (var i=0; i<len; i++) {
- for (var p in arguments[i]) {
- if (arguments[i].hasOwnProperty(p)) {
- ret[p] = arguments[i][p];
- }
- }
- }
- return ret;
- }
-}
-
-
-start =
- top
-
-top
- = "state" "(" p:params ")" { return { state: p } }
- / "selector" "(" p:params ")" { return { selector: p } }
- / "state=" v:value { return { state: v } }
- / "selector=" v:value { return { selector: v } }
-
-params
- = k1: key_value_pair k2:("," key_value_pair)*
- {
- var f = k1;
- for( var i = 0; i < k2.length; i++ ) {
- f = collect(f, k2[i][1])
- }
- return f;
- }
-
-key_value_pair
- = "refinedBy=selector(" p:params ")"
- {
- return { refinedBy: p }
- }
- / "refinedBy=state(" p:params ")"
- {
- return { refinedBy: p }
- }
- / "startSelector=selector(" p:params ")"
- {
- return { startSelector: p };
- }
- / "endSelector=selector(" p:params ")"
- {
- return { endSelector: p };
- }
- / key:key "=" value:value
- {
- var f = {};
- var num = Number(value);
- f[key] = isNaN(num) ? decodeURIComponent(value): num;
- return f;
- }
-
-key
- = atom
-
-value
- = atom
-
-atom
- = chars:validchar+ { return chars.join(""); }
-
-validchar
- = [a-zA-Z0-9\<\>\/\[\]\:%+@.\-!\$\&\;*_]
http://git-wip-us.apache.org/repos/asf/incubator-annotator/blob/4bf46fea/packages/fragment-parser/index.js
----------------------------------------------------------------------
diff --git a/packages/fragment-parser/index.js b/packages/fragment-parser/index.js
deleted file mode 100644
index 47e177a..0000000
--- a/packages/fragment-parser/index.js
+++ /dev/null
@@ -1,18 +0,0 @@
-// @flow
-/**
- * @license
- * 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.
- */
-
-import fragment from './fragment.pegjs';
-export const { SyntaxError, parse } = fragment;
http://git-wip-us.apache.org/repos/asf/incubator-annotator/blob/4bf46fea/packages/fragment-parser/package.json
----------------------------------------------------------------------
diff --git a/packages/fragment-parser/package.json b/packages/fragment-parser/package.json
deleted file mode 100644
index 3ecbc87..0000000
--- a/packages/fragment-parser/package.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
- "name": "@annotator/fragment-parser",
- "description": "Parse Web Annotation URI fragments.",
- "author": "Apache Software Foundation",
- "license": "Apache-2.0",
- "homepage": "https://annotator.apache.org",
- "repository": {
- "type": "git",
- "url": "https://git-wip-us.apache.org/repos/asf/incubator-annotator.git"
- },
- "version": "0.0.0"
-}