You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by pd...@apache.org on 2018/08/08 22:37:56 UTC
[incubator-openwhisk-website] branch master updated: Importing code
from scripts instead of hardcoding in html file (#288)
This is an automated email from the ASF dual-hosted git repository.
pdesai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-website.git
The following commit(s) were added to refs/heads/master by this push:
new 16c0257 Importing code from scripts instead of hardcoding in html file (#288)
16c0257 is described below
commit 16c0257b09cb5e1b099348a7ed21e33e3c31693b
Author: Priti Desai <pd...@us.ibm.com>
AuthorDate: Wed Aug 8 15:37:53 2018 -0700
Importing code from scripts instead of hardcoding in html file (#288)
* loading code from scripts
* fixing space issues with code snippet
---
_layouts/documentation.html | 195 ++++++++++++----------------------------
_plugins/remote_file_content.rb | 36 ++++++++
_plugins/remote_markdown.rb | 48 ----------
3 files changed, 91 insertions(+), 188 deletions(-)
diff --git a/_layouts/documentation.html b/_layouts/documentation.html
index 09ce439..39e774c 100644
--- a/_layouts/documentation.html
+++ b/_layouts/documentation.html
@@ -461,9 +461,9 @@ wsk list</pre>
<ol>
<li style="list-style-type: decimal">Create a file named <i>hello.js</i>:</li>
<div class="terminal">
-<pre>function main() {
- return {payload: 'Hello world'};
-}</pre>
+{% highlight javascript linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/hello.js %}
+{% endhighlight %}
</div>
<li style="list-style-type: decimal">Create an action called <i>helloJS</i> using <i>hello.js</i>:</li>
<div class="terminal">
@@ -493,24 +493,16 @@ wsk list</pre>
<ol>
<li>Create <i>manifest.yaml</i> with the following YAML content:</li>
<div class="terminal">
-<pre>packages:
- helloworld:
- actions:
- helloJS:
- function: hello.js</pre>
+{% highlight yaml linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/manifest-for-helloJS-1.yaml %}
+{% endhighlight %}
</div>
Or
<p></p>
<div class="terminal">
-<pre>packages:
- helloworld:
- actions:
- helloJS:
- code: |
- function main() {
- return {payload: 'Hello world'};
- }
- runtime: nodejs:6</pre>
+{% highlight yaml linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/manifest-for-helloJS-2.yaml %}
+{% endhighlight %}
</div>
<li>Run deployment with <i>wskdeploy</i>:</li>
<div class="terminal">
@@ -558,15 +550,9 @@ wsk list</pre>
<ol>
<li style="list-style-type: decimal">Create a file named <i>hello.py</i>:</li>
<div class="terminal">
-<pre>import sys
-def main(dict):
- if 'name' in dict:
- name = dict['name']
- else:
- name = "stranger"
- greeting = "Hello " + name + "!"
- print(greeting)
- return {"greeting": greeting}</pre>
+{% highlight python linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/hello.py %}
+{% endhighlight %}
</div>
<li style="list-style-type: decimal">Create an action called <i>helloPy</i> using <i>hello.py</i>:</li>
<div class="terminal">
@@ -592,30 +578,16 @@ def main(dict):
<ol>
<li>Create <i>manifest.yaml</i> with the following YAML content:</li>
<div class="terminal">
-<pre>packages:
- helloworld:
- actions:
- helloPy:
- function: hello.py</pre>
+{% highlight yaml linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/manifest-for-helloPy-1.yaml %}
+{% endhighlight %}
</div>
Or
<p></p>
<div class="terminal">
-<pre>packages:
- helloworld:
- actions:
- helloPy:
- code: |
- import sys
- def main(dict):
- if 'name' in dict:
- name = dict['name']
- else:
- name = "stranger"
- greeting = "Hello " + name + "!"
- print(greeting)
- return {"greeting": greeting}
- runtime: python:2</pre>
+{% highlight yaml linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/manifest-for-helloPy-2.yaml %}
+{% endhighlight %}
</div>
<li>Run deployment with <i>wskdeploy</i>:</li>
<div class="terminal">
@@ -662,31 +634,9 @@ def main(dict):
<ol>
<li style="list-style-type: decimal">Create a file named <i>hello.go</i>:</li>
<div class="terminal">
-<pre>package main
-
-import "encoding/json"
-import "fmt"
-import "os"
-
-func main() {
- //program receives one argument: the JSON object as a string
- arg := os.Args[1]
-
- // unmarshal the string to a JSON object
- var obj map[string]interface{}
- json.Unmarshal([]byte(arg), &obj)
-
- // can optionally log to stdout (or stderr)
- fmt.Println("hello Go action")
-
- name, ok := obj["name"].(string)
- if !ok { name = "Stranger" }
-
- // last line of stdout is the result JSON object as a string
- msg := map[string]string{"msg": ("Hello, " + name + "!")}
- res, _ := json.Marshal(msg)
- fmt.Println(string(res))
-}</pre>
+{% highlight go linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/hello.go %}
+{% endhighlight %}
</div>
<li style="list-style-type: decimal">Create an executable called <i>exec</i> using <i>hello.go</i>:</li>
<div class="terminal">
@@ -745,25 +695,9 @@ zip exec.zip exec</pre>
<ol>
<li style="list-style-type: decimal">Create a file named <i>Hello.java</i>:</li>
<div class="terminal">
-<pre>package hello;
-
-import com.google.gson.JsonObject;
-
-public class Hello {
- public static JsonObject main(JsonObject args)
- String name;
-
- try {
- name = args.getAsJsonPrimitive("name").getAsString();
- } catch(Exception e) {
- name = "stranger";
- }
-
- JsonObject response = new JsonObject();
- response.addProperty("greeting", "Hello " + name + "!");
- return response;
- }
-}</pre>
+{% highlight java linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/Hello.java %}
+{% endhighlight %}
</div>
<li style="list-style-type: decimal">Compile <i>Hello.Java</i> into a JAR file <i>hello.jar</i> as follows</li>
<div class="terminal">
@@ -850,14 +784,9 @@ jar cvf hello.jar Hello.class</pre>
<ol>
<li style="list-style-type: decimal">Create a file named <i>hello.php</i>:</li>
<div class="terminal">
-<pre><?php
-function main(array $args) : array
-{
- $name = $args["name"] ?? "stranger";
- $greeting = "Hello $name!";
- echo $greeting;
- return ["greeting" => $greeting];
-}</pre>
+{% highlight php linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/hello.php %}
+{% endhighlight %}
</div>
<li style="list-style-type: decimal">Create an action called <i>helloPHP</i> using <i>hello.php</i>:</li>
<div class="terminal">
@@ -883,29 +812,16 @@ function main(array $args) : array
<ol>
<li>Create <i>manifest.yaml</i> with the following YAML content:</li>
<div class="terminal">
-<pre>packages:
- helloworld:
- actions:
- helloPHP:
- function: hello.php</pre>
+{% highlight yaml linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/manifest-for-helloPhp-1.yaml %}
+{% endhighlight %}
</div>
Or
<p></p>
<div class="terminal">
-<pre>packages:
- helloworld:
- actions:
- helloPHP:
- code: |
- <?php
- function main(array $args) : array
- {
- $name = $args["name"] ?? "stranger";
- $greeting = "Hello $name!";
- echo $greeting;
- return ["greeting" => $greeting];
- }
- runtime: php:7.1</pre>
+{% highlight yaml linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/manifest-for-helloPhp-2.yaml %}
+{% endhighlight %}
</div>
<li>Run deployment with <i>wskdeploy</i>:</li>
<div class="terminal">
@@ -977,11 +893,11 @@ function main(array $args) : array
<ul>
<li>Get a list of packages:</li>
<div class="terminal">
- <pre>wsk package list /whisk.system</pre>
+<pre>wsk package list /whisk.system</pre>
</div>
<li>Get a list of entities in a package:</li>
<div class="terminal">
- <pre>wsk package get --summary /whisk.system/<package-name></pre>
+<pre>wsk package get --summary /whisk.system/<package-name></pre>
</div>
</ul>
<p>
@@ -995,48 +911,48 @@ function main(array $args) : array
<ul>
<li style="list-style: circle">Get a description of the /whisk.system/samples/greeting action:</li>
<div class="terminal">
- <pre>wsk action get --summary /whisk.system/samples/greeting</pre>
+<pre>wsk action get --summary /whisk.system/samples/greeting</pre>
</div>
<div class="terminal">
- <pre>action /whisk.system/samples/greeting: Returns a friendly greeting
- (parameters: name, place)</pre>
+<pre>action /whisk.system/samples/greeting: Returns a friendly greeting
+ (parameters: name, place)</pre>
</div>
<li style="list-style: circle">Invoking action in a package:</li>
<div class="terminal">
- <pre>wsk action invoke --result /whisk.system/samples/greeting</pre>
+<pre>wsk action invoke --result /whisk.system/samples/greeting</pre>
</div>
<div class="terminal">
- <pre>{
- "payload": "Hello, stranger from somewhere!"
- }</pre>
+<pre>{
+ "payload": "Hello, stranger from somewhere!"
+}</pre>
</div>
<li style="list-style: circle">Invoke the action with parameters:</li>
<div class="terminal">
- <pre>wsk action invoke --result /whisk.system/samples/greeting --param name Bernie --param place Vermont</pre>
+<pre>wsk action invoke --result /whisk.system/samples/greeting --param name Bernie --param place Vermont</pre>
</div>
<div class="terminal">
- <pre>{
- "payload": "Hello, Bernie from Vermont!"
- }</pre>
+<pre>{
+ "payload": "Hello, Bernie from Vermont!"
+}</pre>
</div>
</ul>
<li>Creating package binding and invoking action from the binding:</li>
<ul>
<li style="list-style: circle">Creating and using package bindings:</li>
<div class="terminal">
- <pre>wsk package bind /whisk.system/samples valhallaSamples --param place Valhalla</pre>
+<pre>wsk package bind /whisk.system/samples valhallaSamples --param place Valhalla</pre>
</div>
<div class="terminal">
- <pre>ok: created binding valhallaSamples</pre>
+<pre>ok: created binding valhallaSamples</pre>
</div>
<li style="list-style: circle">Invoke an action in the package binding:</li>
<div class="terminal">
- <pre>wsk action invoke --result valhallaSamples/greeting --param name Odin</pre>
+<pre>wsk action invoke --result valhallaSamples/greeting --param name Odin</pre>
</div>
<div class="terminal">
- <pre>{
- "payload": "Hello, Odin from Valhalla!"
- }</pre>
+<pre>{
+ "payload": "Hello, Odin from Valhalla!"
+}</pre>
</div>
</ul>
</ul>
@@ -1100,10 +1016,9 @@ function main(array $args) : array
<li>Please make sure you have the <i>hello</i> action created. If not, you can follow instruction in the section
<a href="#nodejs-actions">above</a> with the following <i>hello.js</i>.</li>
<div class="terminal">
-<pre>
-function main(params) {
- return {payload: 'Hello, ' + params.name + ' from ' + params.place};
-}</pre>
+{% highlight javascript linenos %}
+{% remotefile https://raw.githubusercontent.com/apache/incubator-openwhisk-website/master/code/hello-with-params.js %}
+{% endhighlight %}
</div>
<div class="terminal">
<pre>wsk action create hello hello.js</pre>
diff --git a/_plugins/remote_file_content.rb b/_plugins/remote_file_content.rb
new file mode 100644
index 0000000..d42a874
--- /dev/null
+++ b/_plugins/remote_file_content.rb
@@ -0,0 +1,36 @@
+require 'net/http'
+require 'uri'
+
+module Jekyll
+
+ class RemoteFileContent < Liquid::Tag
+
+ def initialize(tag_name, markup, tokens)
+ url = markup
+
+ puts 'Fetching content of url: ' + url
+
+ if url =~ URI::regexp
+ @content = fetchContent(url)
+ else
+ raise 'Invalid URL passed to RemoteFileContent'
+ end
+
+ super
+ end
+
+ def render(context)
+ if @content
+ @content
+ else
+ raise 'Something went wrong in RemoteFileContent'
+ end
+ end
+
+ def fetchContent(url)
+ Net::HTTP.get(URI.parse(URI.encode(url.strip)))
+ end
+ end
+end
+
+Liquid::Template.register_tag('remotefile', Jekyll::RemoteFileContent)
\ No newline at end of file
diff --git a/_plugins/remote_markdown.rb b/_plugins/remote_markdown.rb
deleted file mode 100644
index 389c93c..0000000
--- a/_plugins/remote_markdown.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-# "THE BEER-WARE LICENSE" (Revision 42):
-# <ro...@gw-computing.net> wrote this file. As long as you retain this
-# notice you can do whatever you want with this stuff. If we meet some day, and
-# you think this stuff is worth it, you can buy me a beer in return.
-# Robin Hahling
-
-require 'net/http'
-
-module Jekyll
- # Remotely fetch a markdown file.
- class RemoteMarkdownTag < Liquid::Tag
- def initialize(tag_name, text, tokens)
- super
-
- text.strip!
- check_protocol(text)
- uri = URI(text)
-
- #check_extension(uri.path)
-
- res = Net::HTTP.get_response(uri)
- fail 'resource unavailable' unless res.is_a?(Net::HTTPSuccess)
-
- body = res.body.force_encoding("UTF-8")
- body.sub! '/<head>(.*)<\/head>/', ''
- @content = body
- end
-
- def render(_context)
- @content
- end
-
- private
-
- def check_protocol(text)
- error_message = "remote_markdown: invalid URI given #{text}"
- fail error_message unless text =~ URI.regexp(%w(http https ftp ftps))
- end
-
- def check_extension(path)
- mdexts = %w(.markdown .mkdown .mkdn .mkd .md)
- error_message = "remote_markdown: URI file extension not in #{mdexts}"
- fail error_message unless mdexts.include?(File.extname(path))
- end
- end
-end
-
-Liquid::Template.register_tag('remote_markdown', Jekyll::RemoteMarkdownTag)