From d9e495b553de6d76aaf6c4735977315e6fb0e51f Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Tue, 12 Sep 2017 11:15:24 -0400 Subject: [PATCH] 10349: Add support for syntax highlighting inline code blocks. Arvados-DCO-1.1-Signed-off-by: Peter Amstutz --- doc/sdk/go/example.html.textile.liquid | 28 ++++----- doc/sdk/java/example.html.textile.liquid | 32 +++++----- doc/sdk/perl/example.html.textile.liquid | 55 +++++++--------- doc/sdk/python/cookbook.html.textile.liquid | 69 ++++++++++++--------- doc/sdk/python/example.html.textile.liquid | 28 ++++----- doc/sdk/ruby/example.html.textile.liquid | 33 +++++----- doc/zenweb-liquid.rb | 44 +++++++++++-- 7 files changed, 164 insertions(+), 125 deletions(-) diff --git a/doc/sdk/go/example.html.textile.liquid b/doc/sdk/go/example.html.textile.liquid index 45a2e44fa7..a5a109b85b 100644 --- a/doc/sdk/go/example.html.textile.liquid +++ b/doc/sdk/go/example.html.textile.liquid @@ -16,7 +16,7 @@ In these examples, the site prefix is @aaaaa@. h2. Initialize SDK -
+{% codeblock as go %}
 import (
   "git.curoverse.com/arvados.git/sdk/go/arvados"
   "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
@@ -28,49 +28,49 @@ func main() {
     log.Fatalf("Error setting up arvados client %s", err.Error())
   }
 }
-
+{% endcodeblock %} h2. create -
+{% codeblock as go %}
   var collection arvados.Collection
   err := api.Create("collections", Dict{"collection": Dict{"name": "create example"}}, &collection)
-
+{% endcodeblock %} h2. delete -
+{% codeblock as go %}
   var collection arvados.Collection
   err := api.Delete("collections", "aaaaa-4zz18-ccccccccccccccc", Dict{}, &collection)
-
+{% endcodeblock %} h2. get -
+{% codeblock as go %}
   var collection arvados.Collection
   err := api.Get("collections", "aaaaa-4zz18-ccccccccccccccc", Dict{}, &collection)
-
+{% endcodeblock %} h2. list -
+{% codeblock as go %}
   var collection arvados.Collection
   err := api.List("collections", Dict{}, &collection)
-
+{% endcodeblock %} h2. update -
+{% codeblock as go %}
   var collection arvados.Collection
   err := api.Update("collections", "aaaaa-4zz18-ccccccccccccccc", Dict{"collection": Dict{"name": "update example"}}, &collection)
-
+{% endcodeblock %} h2. Get current user -
+{% codeblock as go %}
   var user arvados.User
   err := api.Get("users", "current", Dict{}, &user)
-
+{% endcodeblock %} h2. Example program diff --git a/doc/sdk/java/example.html.textile.liquid b/doc/sdk/java/example.html.textile.liquid index 0eabdcb9e9..bc15b83f41 100644 --- a/doc/sdk/java/example.html.textile.liquid +++ b/doc/sdk/java/example.html.textile.liquid @@ -12,47 +12,47 @@ SPDX-License-Identifier: CC-BY-SA-3.0 h2. Initialize SDK -
+{% codeblock as java %}
 import org.arvados.sdk.Arvados;
-
+{% endcodeblock %} -
+{% codeblock as java %}
     String apiName = "arvados";
     String apiVersion = "v1";
 
     Arvados arv = new Arvados(apiName, apiVersion);
-
+{% endcodeblock %} h2. create -
+{% codeblock as java %}
     Map collection = new HashMap();
     collection.put("name", "create example");
 
     Map params = new HashMap();
     params.put("collection", collection);
     Map response = arv.call("collections", "create", params);
-
+{% endcodeblock %} h2. delete -
+{% codeblock as java %}
     Map params = new HashMap();
     params.put("uuid", uuid);
     Map response = arv.call("collections", "delete", params);
-
+{% endcodeblock %} h2. get -
+{% codeblock as java %}
     params = new HashMap();
     params.put("uuid", userUuid);
     Map response = arv.call("users", "get", params);
-
+{% endcodeblock %} h2. list -
+{% codeblock as java %}
     Map params = new HashMap();
     Map response = arv.call("users", "list", params);
 
@@ -61,11 +61,11 @@ h2. list
 
     Map firstUser = (Map)items.get(0);
     String userUuid = (String)firstUser.get("uuid");
-
+{% endcodeblock %} h2. update -
+{% codeblock as java %}
     Map collection = new HashMap();
     collection.put("name", "update example");
 
@@ -73,11 +73,11 @@ h2. update
     params.put("uuid", uuid);
     params.put("collection", collection);
     Map response = arv.call("collections", "update", params);
-
+{% endcodeblock %} h2. Get current user -
+{% codeblock as java %}
     Map params = new HashMap();
     Map response = arv.call("users", "current", params);
-
+{% endcodeblock %} diff --git a/doc/sdk/perl/example.html.textile.liquid b/doc/sdk/perl/example.html.textile.liquid index 42932a36e0..b51cfe4cb5 100644 --- a/doc/sdk/perl/example.html.textile.liquid +++ b/doc/sdk/perl/example.html.textile.liquid @@ -14,12 +14,10 @@ h2. Initialize SDK Set up an API client user agent: - -

+{% codeblock as perl %}
 use Arvados;
 my $arv = Arvados->new('apiVersion' => 'v1');
-
-
+{% endcodeblock %} The SDK retrieves the list of API methods from the server at run time. Therefore, the set of available methods is determined by the server version rather than the SDK version. @@ -27,60 +25,53 @@ h2. create Create an object: - -
my $test_link = $arv->{'links'}->{'create'}->execute('link' => { 'link_class' => 'test', 'name' => 'test' });
-
-
+{% codeblock as perl %} +my $test_link = $arv->{'links'}->{'create'}->execute('link' => { 'link_class' => 'test', 'name' => 'test' }); +{% endcodeblock %} h2. delete - -
my $some_user = $arv->{'collections'}->{'get'}->execute('uuid' => $collection_uuid);
-
-
+{% codeblock as perl %} +my $some_user = $arv->{'collections'}->{'get'}->execute('uuid' => $collection_uuid); +{% endcodeblock %} h2. get Retrieve an object by ID: - -
my $some_user = $arv->{'users'}->{'get'}->execute('uuid' => $current_user_uuid);
-
-
+{% codeblock as perl %} +my $some_user = $arv->{'users'}->{'get'}->execute('uuid' => $current_user_uuid); +{% endcodeblock %} Get the UUID of an object that was retrieved using the SDK: - -
my $current_user_uuid = $current_user->{'uuid'}
-
-
+{% codeblock as perl %} +my $current_user_uuid = $current_user->{'uuid'} +{% endcodeblock %} h2. list Get a list of objects: - -
my $repos = $arv->{'repositories'}->{'list'}->execute;
+{% codeblock as perl %}
+my $repos = $arv->{'repositories'}->{'list'}->execute;
 print ("UUID of first repo returned is ", $repos->{'items'}->[0], "\n");
-
-
+{% endcodeblock %} h2. update Update an object: - -
my $test_link = $arv->{'links'}->{'update'}->execute(
+{% codeblock as perl %}
+my $test_link = $arv->{'links'}->{'update'}->execute(
         'uuid' => $test_link->{'uuid'},
         'link' => { 'properties' => { 'foo' => 'bar' } });
-
-
+{% endcodeblock %} h2. Get current user Get the User object for the current user: - -
my $current_user = $arv->{'users'}->{'current'}->execute;
-
-
+{% codeblock as perl %} +my $current_user = $arv->{'users'}->{'current'}->execute; +{% endcodeblock %} diff --git a/doc/sdk/python/cookbook.html.textile.liquid b/doc/sdk/python/cookbook.html.textile.liquid index fa059f7028..a046654a64 100644 --- a/doc/sdk/python/cookbook.html.textile.liquid +++ b/doc/sdk/python/cookbook.html.textile.liquid @@ -12,22 +12,25 @@ SPDX-License-Identifier: CC-BY-SA-3.0 h2. Cancel a container request -
import arvados
+{% codeblock as python %}
+import arvados
 arvados.api().container_requests().update(uuid=container_request_uuid, body={"priority": 0}).execute()
-
+{% endcodeblock %} h2. Cancel all container requests -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
 result = api.container_requests().list(filters=[["state", "=", "Committed"], ["priority", ">", "0"]]).execute()["items"]
 for container_request in result:
     api.container_requests().update(uuid=container_request["uuid"], body={"priority": 0}).execute()
-
+{% endcodeblock %} h2. List completed container requests -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
 result = api.container_requests().list(filters=[["name", "like", name], ["state", "=", "Final"]]).execute()["items"]
 container_uuids = [cr["container_uuid"] for cr in result]
@@ -37,32 +40,37 @@ container_dict = {c["uuid"]: c for c in containers}
 for container_request in result:
     container = container_dict[container_request["container_uuid"]]
     print("%s, %s, %s" % (container_request["uuid"], container_request["name"], "Success" if container["exit_code"] == 0 else "Failed"))
-
+{% endcodeblock %} h2. Get input of a CWL workflow -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
+container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz"
 container_request = arvados.api().container_requests().get(uuid=container_request_uuid).execute()
 print(container_request["mounts"]["/var/lib/cwl/cwl.input.json"])
-
+{% endcodeblock %} h2. Get output of a CWL workflow -
import arvados
+{% codeblock as python %}
+import arvados
 import arvados.collection
 api = arvados.api()
+container_request_uuid="qr1hi-xvhdp-zzzzzzzzzzzzzzz"
 container_request = arvados.api().container_requests().get(uuid=container_request_uuid).execute()
 collection = arvados.collection.CollectionReader(container_request["output_uuid"])
 print(collection.open("cwl.output.json").read())
-
+{% endcodeblock %} h2. List input of child requests -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
 parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
-namefilter = "bwa%"
+namefilter = "bwa%"  # the "like" filter uses SQL pattern match syntax
 container_request = arvados.api().container_requests().get(uuid=parent_request_uuid).execute()
 parent_container_uuid = container_request["container_uuid"]
 child_requests = arvados.api().container_requests().list(filters=[
@@ -73,14 +81,15 @@ for c in child_requests["items"]:
     for m in c["mounts"].values():
         if "portable_data_hash" in m:
             print("  %s" % m["portable_data_hash"])
-
+{% endcodeblock %} h2. List output of child requests -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
-parent_request_uuid = "qr1hi-xvhdp-61y7cjymnhyp7yu"
-namefilter = "bwa%"
+parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
+namefilter = "bwa%"  # the "like" filter uses SQL pattern match syntax
 container_request = arvados.api().container_requests().get(uuid=parent_request_uuid).execute()
 parent_container_uuid = container_request["container_uuid"]
 child_requests = arvados.api().container_requests().list(filters=[
@@ -91,13 +100,14 @@ collections = arvados.api().collections().list(filters=[["uuid", "in", output_uu
 uuid_to_pdh = {c["uuid"]: c["portable_data_hash"] for c in collections["items"]}
 for c in child_requests["items"]:
     print("%s -> %s" % (c["name"], uuid_to_pdh[c["output_uuid"]]))
-
+{% endcodeblock %} h2. List failed child requests -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
-parent_request_uuid = "qr1hi-xvhdp-3d8f1zatx1xio36"
+parent_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
 container_request = arvados.api().container_requests().get(uuid=parent_request_uuid).execute()
 parent_container_uuid = container_request["container_uuid"]
 child_requests = arvados.api().container_requests().list(filters=[
@@ -108,38 +118,41 @@ cancelled_child_containers = arvados.api().containers().list(filters=[
     ["uuid", "in", child_containers.keys()]], limit=1000).execute()
 for c in cancelled_child_containers["items"]:
     print("%s (%s)" % (child_containers[c["uuid"]]["name"], child_containers[c["uuid"]]["uuid"]))
-
+{% endcodeblock %} h2. Get log of a child request -
import arvados
+{% codeblock as python %}
+import arvados
 import arvados.collection
 api = arvados.api()
-container_request_uuid = "qr1hi-xvhdp-3d8f1zatx1xio36"
+container_request_uuid = "qr1hi-xvhdp-zzzzzzzzzzzzzzz"
 container_request = arvados.api().container_requests().get(uuid=container_request_uuid).execute()
 collection = arvados.collection.CollectionReader(container_request["log_uuid"])
 for c in collection:
     print(collection.open(c).read())
-
+{% endcodeblock %} h2. Create a collection sharing link -
import arvados
+{% codeblock as python %}
+import arvados
 api = arvados.api()
 download="https://your.download.server"
-collection_uuid="your-4zz18-collectionuuid"
+collection_uuid="qr1hi-4zz18-zzzzzzzzzzzzzzz"
 token = api.api_client_authorizations().create(body={"api_client_authorization":{"scopes": [
     "GET /arvados/v1/collections/%s" % collection_uuid,
     "GET /arvados/v1/collections/%s/" % collection_uuid,
     "GET /arvados/v1/keep_services/accessible"]}}).execute()
 print("%s/c=%s/t=%s/_/" % (download, collection_uuid, token["api_token"]))
-
+{% endcodeblock %} h2. Combine two or more collections Note, if two collections have files of the same name, the contents will be concatenated in the resulting manifest. -
import arvados
+{% codeblock as python %}
+import arvados
 import arvados.collection
 api = arvados.api()
 project_uuid = "qr1hi-tpzed-zzzzzzzzzzzzzzz"
@@ -150,4 +163,4 @@ for u in collection_uuids:
     combined_manifest += c["manifest_text"]
 newcol = arvados.collection.Collection(combined_manifest)
 newcol.save_new(name="My combined collection", owner_uuid=project_uuid)
-
+{% endcodeblock %} diff --git a/doc/sdk/python/example.html.textile.liquid b/doc/sdk/python/example.html.textile.liquid index 6f6ba99ded..504d0784f0 100644 --- a/doc/sdk/python/example.html.textile.liquid +++ b/doc/sdk/python/example.html.textile.liquid @@ -14,43 +14,43 @@ In these examples, the site prefix is @aaaaa@. h2. Initialize SDK -
+{% codeblock as python %}
 import arvados
 api = arvados.api("v1")
-
+{% endcodeblock %} h2. create -
+{% codeblock as python %}
 result = api.collections().create(body={"collection": {"name": "create example"}}).execute()
-
+{% endcodeblock %} h2. delete -
+{% codeblock as python %}
 result = api.collections().delete(uuid="aaaaa-4zz18-ccccccccccccccc").execute()
-
+{% endcodeblock %} h2. get -
+{% codeblock as python %}
 result = api.collections().get(uuid="aaaaa-4zz18-ccccccccccccccc").execute()
-
+{% endcodeblock %} h2. list -
+{% codeblock as python %}
 result = api.collections().list(filters=[["uuid", "=", "aaaaa-bbbbb-ccccccccccccccc"]]).execute()
-
+{% endcodeblock %} h2. update -
+{% codeblock as python %}
 result = api.collections().update(uuid="aaaaa-4zz18-ccccccccccccccc", body={"collection": {"name": "update example"}}).execute()
-
+{% endcodeblock %} h2. Get current user -
+{% codeblock as python %}
 result = api.users().current().execute()
-
+{% endcodeblock %} diff --git a/doc/sdk/ruby/example.html.textile.liquid b/doc/sdk/ruby/example.html.textile.liquid index 621b664fb6..b8c0dcbb80 100644 --- a/doc/sdk/ruby/example.html.textile.liquid +++ b/doc/sdk/ruby/example.html.textile.liquid @@ -14,10 +14,10 @@ h2. Initialize SDK Import the module and set up an API client user agent: -
+{% codeblock as ruby %}
 require 'arvados'
 arv = Arvados.new(apiVersion: 'v1')
-
+{% endcodeblock %} The SDK retrieves the list of API methods from the server at run time. Therefore, the set of available methods is determined by the server version rather than the SDK version. @@ -25,56 +25,57 @@ h2. create Create an object: -
+{% codeblock as ruby %}
 new_link = arv.link.create(link: {link_class: 'test', name: 'test'})
-
+{% endcodeblock %} h2. delete Delete an object: -
+{% codeblock as ruby %}
 arv.link.delete(uuid: new_link[:uuid])
-
+{% endcodeblock %} h2. get Retrieve an object by ID: -
+{% codeblock as ruby %}
 some_user = arv.user.get(uuid: current_user_uuid)
-
+{% endcodeblock %} h2. list Get a list of objects: -
+{% codeblock as ruby %}
 repos = arv.repository.list
 first_repo = repos[:items][0]
 puts "UUID of first repo returned is #{first_repo[:uuid]}"
+{% endcodeblock %}
+
 UUID of first repo returned is qr1hi-s0uqq-b1bnybpx3u5temz
-
h2. update Update an object: -
+{% codeblock as ruby %}
 updated_link = arv.link.update(uuid: new_link[:uuid],
                                link: {properties: {foo: 'bar'}})
-
+{% endcodeblock %} h2. Get current user Get the User object for the current user: -
+{% codeblock as ruby %}
 current_user = arv.user.current
-
+{% endcodeblock %} Get the UUID of an object that was retrieved using the SDK: -
+{% codeblock as ruby %}
 current_user_uuid = current_user[:uuid]
-
+{% endcodeblock %} diff --git a/doc/zenweb-liquid.rb b/doc/zenweb-liquid.rb index 4a6828188e..baa8fe42db 100644 --- a/doc/zenweb-liquid.rb +++ b/doc/zenweb-liquid.rb @@ -16,7 +16,7 @@ module Zenweb def render_liquid page, content liquid self.body, content, page, binding end - + ## # Render a page's liquid and return the intermediate result def liquid template, content, page, binding = TOPLEVEL_BINDING @@ -24,7 +24,7 @@ module Zenweb unless defined? @liquid_template @liquid_template = Liquid::Template.parse(template) end - + vars = {} vars["content"] = content @@ -38,7 +38,7 @@ module Zenweb vars["page"] = page.config.h.clone vars["page"]["url"] = page.url - + @liquid_template.render(vars) end end @@ -57,7 +57,7 @@ module Zenweb raise SyntaxError.new("Error in tag 'code' - Valid syntax: include '[code_file]' as '[language]'") end end - + def render(context) require 'coderay' @@ -71,6 +71,40 @@ module Zenweb html end - Liquid::Template.register_tag('code', LiquidCode) + Liquid::Template.register_tag('code', LiquidCode) + end + + class LiquidCodeBlock < Liquid::Block + Syntax = /((?:as)\s+(#{Liquid::QuotedFragment}+))?/o + + def initialize(tag_name, markup, tokens) + Liquid::Tag.instance_method(:initialize).bind(self).call(tag_name, markup, tokens) + + if markup =~ Syntax + @language = $2 + @attributes = {} + else + raise SyntaxError.new("Error in tag 'code' - Valid syntax: codeblock as '[language]'") + end + end + + def render(context) + require 'coderay' + + partial = super + html = '' + + if partial[0] == '\n' + partial = partial[1..-1] + end + + context.stack do + html = CodeRay.scan(partial, @language).div + end + + "#{html}" + end + + Liquid::Template.register_tag('codeblock', LiquidCodeBlock) end end -- 2.39.5