From 7f39f1d32d1e7edeef04f9166cb7594f73d7c26b Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 17 Oct 2012 11:12:01 +0200 Subject: [PATCH 1/5] Add script to generate example index --- bin/generate-examples-index | 59 +++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 bin/generate-examples-index diff --git a/bin/generate-examples-index b/bin/generate-examples-index new file mode 100755 index 0000000000..935ca11dcb --- /dev/null +++ b/bin/generate-examples-index @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +from contextlib import contextmanager +from optparse import OptionParser +import os +import sys +import xml.etree.cElementTree as ElementTree + + +@contextmanager +def tag(tb, name, attrs=None): + tb.start(name, attrs) + yield tb + tb.end(name) + + +def main(argv): + + option_parser = OptionParser() + option_parser.add_option('--output', '-o', metavar='FILENAME') + options, args = option_parser.parse_args(argv[1:]) + if options.output: + outputdir = os.path.dirname(options.output) + else: + outputdir = os.curdir() + + tb = ElementTree.TreeBuilder() + with tag(tb, 'html'): + with tag(tb, 'head'): + with tag(tb, 'meta', {'content': 'text/html; charset=utf-8', + 'http-equiv': 'Content-Type'}): + pass + with tag(tb, 'link', {'href': 'style.css', + 'rel': 'stylesheet', + 'type': 'text/css'}): + pass + with tag(tb, 'title'): + tb.data('ol3 examples') + with tag(tb, 'body'): + with tag(tb, 'h1'): + tb.data('ol3 examples') + with tag(tb, 'ul'): + for arg in sorted(args): + with tag(tb, 'li'): + href = os.path.relpath(arg, outputdir) + with tag(tb, 'a', {'href': href}): + tb.data(os.path.splitext(os.path.basename(arg))[0] + .replace('-', ' ')) + + if options.output: + output = open(options.output, 'w') + else: + output = sys.stdout + output.write('') + output.write(ElementTree.tostring(tb.close(), 'utf-8', 'html')) + + +if __name__ == '__main__': + sys.exit(main(sys.argv)) From 129578a515cbf1b62d9d4debc71749f14bfecfa7 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 17 Oct 2012 11:12:18 +0200 Subject: [PATCH 2/5] Build examples index --- .gitignore | 1 + Makefile | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 4437261c14..d54075378b 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /build/src /examples/*.json /examples/*.combined.js +/examples/index.html diff --git a/Makefile b/Makefile index bcccf0b198..8053496884 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ EXTERNAL_SRC = \ build/src/external/externs/types.js \ build/src/external/src/exports.js \ build/src/external/src/types.js -EXAMPLES = $(shell find examples -maxdepth 1 -name \*.html) +EXAMPLES = $(filter-out examples/index.html,$(shell find examples -maxdepth 1 -name \*.html)) comma := , empty := space := $(empty) $(empty) @@ -63,7 +63,10 @@ build/src/internal/src/types.js: bin/generate-exports src/ol/exports.txt build-examples: examples $(subst .html,.combined.js,$(EXAMPLES)) .PHONY: examples -examples: $(subst .html,.json,$(EXAMPLES)) +examples: examples/index.html $(subst .html,.json,$(EXAMPLES)) + +examples/index.html: bin/generate-examples-index $(EXAMPLES) + bin/generate-examples-index -o $@ $(EXAMPLES) examples/%.json: Makefile base.json echo "{\"id\": \"$(basename $(notdir $@))\", \"inherits\": \"../base.json\", \"inputs\": [\"$(subst .json,.js,$@)\", \"build/src/internal/src/types.js\"]}" > $@ From f120410d6d53e5c5aedb32905d1ee02de0da380a Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 17 Oct 2012 11:29:41 +0200 Subject: [PATCH 3/5] Add option to hook in scripts to index generator --- bin/generate-examples-index | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bin/generate-examples-index b/bin/generate-examples-index index 935ca11dcb..e29e8159f5 100755 --- a/bin/generate-examples-index +++ b/bin/generate-examples-index @@ -18,6 +18,8 @@ def main(argv): option_parser = OptionParser() option_parser.add_option('--output', '-o', metavar='FILENAME') + option_parser.add_option('--script', '-s', action='append', default=[], + dest='scripts', metavar='SCRIPT') options, args = option_parser.parse_args(argv[1:]) if options.output: outputdir = os.path.dirname(options.output) @@ -46,6 +48,10 @@ def main(argv): with tag(tb, 'a', {'href': href}): tb.data(os.path.splitext(os.path.basename(arg))[0] .replace('-', ' ')) + for script in sorted(options.scripts): + src = os.path.relpath(script, outputdir) + with tag(tb, 'script', {'src': src, 'type': 'text/javascript'}): + tb.data('') if options.output: output = open(options.output, 'w') From e9dbb00039001d74b76eaba2b98b17cdec3a62aa Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 17 Oct 2012 11:30:21 +0200 Subject: [PATCH 4/5] Pass query string through example index This is so that visiting /examples/index.html?mode=RAW&Debug=true will automatically add ?mode=RAW&Debug=true to all links in the generated index. --- Makefile | 2 +- examples/index.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 examples/index.js diff --git a/Makefile b/Makefile index 8053496884..0f1946f2df 100644 --- a/Makefile +++ b/Makefile @@ -66,7 +66,7 @@ build-examples: examples $(subst .html,.combined.js,$(EXAMPLES)) examples: examples/index.html $(subst .html,.json,$(EXAMPLES)) examples/index.html: bin/generate-examples-index $(EXAMPLES) - bin/generate-examples-index -o $@ $(EXAMPLES) + bin/generate-examples-index -o $@ -s examples/index.js $(EXAMPLES) examples/%.json: Makefile base.json echo "{\"id\": \"$(basename $(notdir $@))\", \"inherits\": \"../base.json\", \"inputs\": [\"$(subst .json,.js,$@)\", \"build/src/internal/src/types.js\"]}" > $@ diff --git a/examples/index.js b/examples/index.js new file mode 100644 index 0000000000..21bc8fa4f4 --- /dev/null +++ b/examples/index.js @@ -0,0 +1,16 @@ + + +/** + * Loader to append the query string to every A element in the document. + * + * This is so that (for example) visiting + * /examples/index.html?mode=RAW&Debug=true + * will cause all links to automatically include the ?mode=RAW&Debug=true. + */ +(function() { + var as = document.getElementsByTagName('a'); + var i, n = as.length; + for (i = 0; i < n; ++i) { + as[i].href += window.location.search; + } +})(); From 0e81f67a00f4b06e0966d67e00cbca80fa95789c Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 17 Oct 2012 11:36:54 +0200 Subject: [PATCH 5/5] Host example index --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index 0f1946f2df..19588fefc0 100644 --- a/Makefile +++ b/Makefile @@ -119,6 +119,7 @@ hostexamples: build examples cp $(EXAMPLES) $(subst .html,.js,$(EXAMPLES)) examples/style.css build/gh-pages/$(BRANCH)/examples/ cp build/loader_hosted_examples.js build/gh-pages/$(BRANCH)/examples/loader.js cp build/ol.js build/ol.css build/gh-pages/$(BRANCH)/build/ + cp examples/index.html examples/index.js build/gh-pages/$(BRANCH)/examples .PHONY: test test: $(INTERNAL_SRC)