|
| 1 | +# Quick Start Guide |
| 2 | + |
| 3 | +For more in depth examples, you can read our quick start guide |
| 4 | +[here](https://www.gopherguides.com/articles/golang-hype-quickstart). |
| 5 | + |
| 6 | +# The Basics |
| 7 | + |
| 8 | +This is the syntax to include a code sample in your document: |
| 9 | + |
| 10 | +``` |
| 11 | +<code src="src/hello/main.go" snippet="example"></code> |
| 12 | +``` |
| 13 | + |
| 14 | +The above code snippet does the following: |
| 15 | + |
| 16 | +- Includes the code snippet specified in the source code |
| 17 | +- Validates that the code compiles |
| 18 | + |
| 19 | +Here is the source file: |
| 20 | + |
| 21 | +```go |
| 22 | +package main |
| 23 | + |
| 24 | +import "fmt" |
| 25 | + |
| 26 | +// snippet: example |
| 27 | +func main() { |
| 28 | + fmt.Println("Hello World") |
| 29 | +} |
| 30 | + |
| 31 | +// snippet: example |
| 32 | +``` |
| 33 | + |
| 34 | +Notice the use of the `snippet` comment. The format for the comment is: |
| 35 | + |
| 36 | +``` |
| 37 | +// snippet: <snippet_name_here> |
| 38 | +``` |
| 39 | + |
| 40 | +You must have a beginning and an ending snippet for the code to work. |
| 41 | + |
| 42 | +The output of including that tag will be as follows: |
| 43 | + |
| 44 | +<code src="src/hello/main.go" snippet="example"></code> |
| 45 | + |
| 46 | +A `snippet` is not required in your `code` tag. They default behavior of a `code` tag is to include the entire source file. |
| 47 | + |
| 48 | +If we leave the tag out, it will result in the following code being included: |
| 49 | + |
| 50 | +<code src="src/hello/main.go"></code> |
| 51 | + |
| 52 | +Notice that none of the `snippet` comments were in the output? This is because hype recognizes them as directives for the document, and will not show them in the actual output. |
| 53 | + |
| 54 | +# Go Specific Commands |
| 55 | + |
| 56 | +There are a number of [Go](https://go.dev/) specific commands you can run as well. Anything from executing the code and showing the output, to including go doc (from the standard library or your own source code), etc. |
| 57 | + |
| 58 | +Let's look at how we use the `go` tag. |
| 59 | + |
| 60 | +Here is the source code of the Go file we are going to include. Notice the use of the `snippet` comments to identify the area of code we want included. We'll see how to specify that in the next section when we include it in our markdown. |
| 61 | + |
| 62 | +# Running Go Code |
| 63 | + |
| 64 | +The following command will include the go source code, run it, and include the output of the program as well: |
| 65 | + |
| 66 | +``` |
| 67 | +<go src="src/hello" run="."></go> |
| 68 | +``` |
| 69 | +Here is the result that will be included in your document from the above command: |
| 70 | + |
| 71 | +<go src="src/hello" run="."></go> |
| 72 | + |
| 73 | +## Running and Showing the Code |
| 74 | + |
| 75 | +If you want to both run and show the code with the same tag, you can add the `code` attribute to the tag: |
| 76 | + |
| 77 | +``` |
| 78 | +<go src="src/hello" run="." code="main.go"></go> |
| 79 | +``` |
| 80 | + |
| 81 | +Now the source code is includes, as well as the output of the program: |
| 82 | + |
| 83 | +<go src="src/hello" run="." code="main.go"></go> |
| 84 | + |
| 85 | +## Snippets with Go |
| 86 | + |
| 87 | +You can also specify the snippet in a `go` tag as well. The result is that it will only include the code snippet in the included source: |
| 88 | + |
| 89 | +``` |
| 90 | +<go src="src/hello" run="." code="main.go#example"></go> |
| 91 | +``` |
| 92 | + |
| 93 | +You can see now that only the snippet is included, but the output is still the same: |
| 94 | + |
| 95 | +<go src="src/hello" run="." code="main.go#example"></go> |
| 96 | + |
| 97 | +## Invalid Code |
| 98 | + |
| 99 | +What if you want to include an example of code that does not compile? We still want the code to be parsed and included, even though the code doesn't compile. For this, we can state the expected output of the program. |
| 100 | + |
| 101 | +``` |
| 102 | +<go src="src/broken" run="." code="main.go#example" exit="1"></go> |
| 103 | +``` |
| 104 | + |
| 105 | +The result now includes the snippet, and the error output from trying to compile the invalid source code. |
| 106 | + |
| 107 | +<go src="src/broken" run="." code="main.go#example" exit="1"></go> |
| 108 | + |
| 109 | +### GoDoc |
| 110 | + |
| 111 | +While there are a number of `godoc` commands that will allow you to put your documentation from your code directly into your articles as well. Here are some of the commands. |
| 112 | + |
| 113 | +Here is the basic usage first: |
| 114 | + |
| 115 | +``` |
| 116 | +<go doc="-short context"></go> |
| 117 | +``` |
| 118 | + |
| 119 | +Here is the output for the above command: |
| 120 | + |
| 121 | +<go doc="-short context"></go> |
| 122 | + |
| 123 | +You can also be more specific. |
| 124 | + |
| 125 | +``` |
| 126 | +<go doc="-short context.WithCancel"></go> |
| 127 | +``` |
| 128 | + |
| 129 | +Here is the output for the above command: |
| 130 | +<go doc="-short context.WithCancel"></go> |
| 131 | + |
| 132 | +For more examples, see the [hype repo](https://www.github.com/gopherguides/hype). |
| 133 | + |
| 134 | + |
| 135 | +# Arbitrary Commands |
| 136 | + |
| 137 | +You can also use the `cmd` tag and the `exec` attribute to run arbitrary commands and include them in your documentation. Here is the command to run the `tree` command and include it in our documentation: |
| 138 | + |
| 139 | +``` |
| 140 | +<cmd exec="tree" src="."></cmd> |
| 141 | +``` |
| 142 | + |
| 143 | +Here is the output: |
| 144 | + |
| 145 | +<cmd exec="tree" src="."></cmd> |
| 146 | + |
| 147 | +# The Export Command |
| 148 | + |
| 149 | +There are several options for running the `hype` command. Most notable is the `export` option: |
| 150 | + |
| 151 | +``` |
| 152 | +$ hype export -h |
| 153 | +
|
| 154 | +Usage of hype: |
| 155 | + -f string |
| 156 | + optional file name to preview, if not provided, defaults to module.md (default "module.md") |
| 157 | + -format string |
| 158 | + content type to export to: markdown, html (default "markdown") |
| 159 | + -timeout duration |
| 160 | + timeout for execution, defaults to 30 seconds (30s) (default 5s) |
| 161 | + -v enable verbose output for debugging |
| 162 | +
|
| 163 | +Usage: hype export [options] |
| 164 | +
|
| 165 | +Examples: |
| 166 | + hype export -format html |
| 167 | + hype export -f README.md -format html |
| 168 | + hype export -f README.md -format markdown -timeout=10s |
| 169 | +``` |
| 170 | + |
| 171 | +This allows you to see your compiled document either as a single markdown, or as an html document that you can preview in the browser. |
| 172 | + |
| 173 | +# Including Markdown |
| 174 | + |
| 175 | +To include a markdown file, use the include tag. This will run that markdown file through the hype.Parser being used and append the results to the current document. |
| 176 | + |
| 177 | +The paths specified in the src attribute of the include are relative to the markdown file they are used in. This allows you to move entire directory structures around in your project without having to change references within the documents themselves. |
| 178 | + |
| 179 | +The following code will parse the code/code.md and sourceable/sourceable.md documents and append them to the end of the document they were included in. |
| 180 | + |
| 181 | +<code src="includes.md"></code> |
| 182 | + |
| 183 | + |
0 commit comments