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

adding some handlebar examples to README #290

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,125 @@ The built in compiler remains solc 0.6, but users who want to use the newer comp
```
npm install -D solc-0.7@npm:solc@^0.7.0
npx solidity-docgen --solc-module solc-0.7
```


## Example Handlebars Formatting
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Example Handlebars Formatting
## Example Handlebars Templates
You can customize the output of solidity-docgen through Handlebars templates, using the `-t` argument in the command line with the path to the directory where the `contract.hbs` template file is located. For example, `solidity-docgen -t templates`.


Each of the following examples will go through various methods exposed by the solidity-docgen library, how to use them in handlebars, and what the resulting markdown output would look like.

1. Getting the natspec @dev and @notice tags
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is getting rendered as a numbered list and I think we want them to look more like headings, so I would go with:

Suggested change
1. Getting the natspec @dev and @notice tags
### 1. Getting the natspec @dev and @notice tags

And we should increase the nesting of the two subsections in each of these.


### Handlebars formatting:

``` {{{natspec.userdoc}}}
{{{natspec.devdoc}}}
```
Comment on lines +46 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub doesn't let me leave a suggestion for this, but the first line of each of these code blocks is not being displayed in the Markdown rendering. The first line needs to be on its own line, like so:

```handlebars
{{{natspec.userdoc}}}
{{{natspec.devdoc}}}
```

If we add handlebars at the top it will probably display with some syntax highlighting.


### Markdown output:

This is the comment next to the @notice tag.
This is the comment next to the @dev tag.
Comment on lines +52 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should display the output in a code block as well, so like:

This is the comment next to the @notice tag.
This is the comment next to the @dev tag. 



2. Functions and Events
Your handlebars template can check if the underlying contract code is a function or an event with {{#if ownFunctions}} and {{#if ownEvents}} respectively. This lets you generate different markdown depending on if the underlying natspec is for functions or events. Use {{ownFunctions}} and {{ownEvents}} to iterate over all functions and events in the contract respectively.

### Handlebars formatting:
``` {{#if ownFunctions}
### Functions
{{/if}}
{{#ownFunctions}}
- function {{name}}
{{/ownFunctions}}
{{/if ownFunctions}}
```


### Markdown Output:

### Functions
- function example1
- function example2
- function example3

3. Getting more data about a funcion

In 2. we saw how to iterate over the functions in a contract and each of the function names. Here is an example that gets more information related to a function including the input parameters, and visibility.

### Handlebars formatting:

``` {{#if ownFunctions}
### Functions
{{/if}}
{{#ownFunctions}}
#### function {{name}}
Parameters:
{{natspec.params}}
- {{param}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to also include the description.

Suggested change
- {{param}}
- {{param}}: {{description}}

{{/natspec.params}}
Visibility:
- {{visibility}}
{{#if outputs}}
Outputs:
- {{outputs}}
{{/if}}
{{/ownFunctions}}
{{/if ownFunctions}}
```

### Markdown Output:

### Functions
#### function example1
Parameters:
- param1
- param2
Visibliity:
- external
Outputs:
- bool return1

#### function example2
Parameters:
- param1
- param2
- param3
Visibliity:
- internal
Outputs:
- uint128 return0, uint128 return1

#### function example3
Parameters:
- param1
Visibliity:
- external
Outputs:
- int24 return1

4. Formatting a function signature
The nice thing about handlebars templating is that it just populates data into a markdown format. So you can use any markdown syntax to arrange your data such that it looks pretty when the markdown is generated! (The below does some fancy handlebar manipulation that you don't really need to understand. It essentially is populating the types of the parameters so that the output in markdown looks like a function signature.)

### Handlebars formatting:
```
### {{name}}
```solidity
function {{name}}(
{{#natspec.params}}
{{#lookup ../args.types @index}}{{/lookup}} {{param}}{{#if @last}}{{else}},{{/if}}
{{/natspec.params}}
) {{visibility}}{{#if outputs}} returns ({{outputs}}){{/if}}```

```

### Markdown Output:
Here is what the output for the example1 function would look like.

### example1
```solidity
function example1(
int24 param1,
int24 param2
) external returns (bool return1)
```