Skip to content
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

Update readfile shortcode #995

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions layouts/shortcodes/readfile.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{/* Handle the "file" named parameter or a single unnamed parameter as the file
path */}}
{{ if .IsNamedParams }}
{{ $.Scratch.Set "fparameter" ( .Get "file" ) }}
{{ else }}
{{ $.Scratch.Set "fparameter" ( .Get 0 ) }}
{{ end }}


{{/* If the first character is "/", the path is absolute from the site's
`baseURL`. Otherwise, construct an absolute path using the current directory */}}

{{ if eq (.Scratch.Get "fparameter" | printf "%.1s") "/" }}
{{ $.Scratch.Set "filepath" ($.Scratch.Get "fparameter") }}
{{ else }}
{{ $.Scratch.Set "filepath" "/" }}
{{ $.Scratch.Add "filepath" $.Page.File.Dir }}
{{ $.Scratch.Add "filepath" ($.Scratch.Get "fparameter") }}
{{ end }}


{{/* If the file exists, read it and highlight it if it's code. Throw an error
if the file is not found */}}

{{ if fileExists ($.Scratch.Get "filepath") }}
{{ if eq (.Get "code") "true" }}
{{- highlight ($.Scratch.Get "filepath" | readFile | htmlUnescape |
safeHTML ) (.Get "lang") "" -}}
{{ else }}
{{- $.Scratch.Get "filepath" | readFile | htmlUnescape | safeHTML -}}
{{ end }}
{{ else }}

<p style="color: #D74848"><b><i>The file <code>{{ $.Scratch.Get "filepath" }}</code> was not found.</i></b></p>

{{ end }}
8 changes: 0 additions & 8 deletions layouts/shortcodes/readfile.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: hello
spec:
steps:
- name: echo
image: alpine
script: |
#!/bin/sh
echo "Hello World"
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
**Installation**

1. Download the installation files.

1. Run the installation script

`sudo sh install.sh`

1. Test that your installation was successfully completed.

104 changes: 104 additions & 0 deletions userguide/content/en/docs/Adding content/Shortcodes/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,107 @@ File[] hiddenFiles = new File("directory_name")
{{< /card-code >}}
{{< /cardpane >}}

## Include external files

Sometimes there's content that is relevant for several documents, or that is
maintained in a file that is not necessarily a document. For situations like
these, the `readfile` shortcode allows you to import the contents of an external
file into a document.

### Reuse documentation

In case you want to reuse some content in several documents, you can write said
content in a separate file and include it wherever you need it.

For example, suppose you have a file called `installation.md` with the following
contents:

```go-html-template
## Installation

1. Download the installation files.

1. Run the installation script

`sudo sh install.sh`

1. Test that your installation was successfully completed.

```

You can import this section into another document:

```go-html-template
The following section explains how to install the database:

{{%/* readfile "installation.md" */%}}

```

This will be rendered as if the instructions were in the parent document:

---

The following section explains how to install the database:

{{% readfile "includes/installation.md" %}}

---

The parameter is the relative path to the file. Only relative paths
under the parent file's working directory are supported.

For files outside the current working directory you can use an absolute path
starting with `/`. The root directory is the `/content` folder.

### Include code files

Suppose you have an `includes` folder containing several code samples you want
to use as part of your documentation. You can use `readfile` with some
additional parameters:

```go-html-template
To create a new pipeline, follow the next steps:

1. Create a configuration file `config.yaml`:

{{</* readfile file="includes/config.yaml" code="true" lang="yaml" */>}}

1. Apply the file to your cluster `kubectl apply config.yaml`

```

This code automatically reads the content of `import/config.yaml` and inserts it
into the document. The rendered text looks like this:

---

To create a new pipeline, follow the next steps:

1. Create a configuration file `config.yaml`:

{{< readfile file="includes/config.yaml" code="true" lang="yaml" >}}

1. Apply the file to your cluster `kubectl apply config.yaml`

---

{{% alert title="Warning" color="warning" %}}
You must use `{{</* */>}}` delimiters for the code highlighting to work
correctly.
{{% /alert %}}

The "file" parameter is the relative path to the file. Only relative paths
under the parent file's working directory are supported.

For files outside the current working directory you can use an absolute path
starting with `/`. The root directory is the `/content` folder.



| Parameter | Default | Description |
| ---------------- |------------| ------------|
| file | | Path of external file|
| code | false | Boolean value. If `true` the contents is treated as code|
| lang | plain text | Programming language |