|
| 1 | +.. _GenerateDocumentation: |
| 2 | + |
| 3 | +GenerateDocumentation |
| 4 | +================================ |
| 5 | +Generate keyword documentation with the documentation template. |
| 6 | + |
| 7 | +.. |TRANSFORMERNAME| replace:: GenerateDocumentation |
| 8 | +.. include:: disabled_hint.txt |
| 9 | + |
| 10 | + |
| 11 | +By default, GenerateDocumentation uses |
| 12 | +`Google docstring <https://google.github.io/styleguide/pyguide.html#383-functions-and-methods>`_ |
| 13 | +as the documentation template. |
| 14 | + |
| 15 | +.. tab-set:: |
| 16 | + |
| 17 | + .. tab-item:: Before |
| 18 | + |
| 19 | + .. code:: robotframework |
| 20 | +
|
| 21 | + *** Keywords *** |
| 22 | + Keyword |
| 23 | + [Arguments] ${arg} |
| 24 | + ${var} ${var2} Step |
| 25 | + RETURN ${var} ${var2} |
| 26 | +
|
| 27 | + Keyword With ${embedded} Variable |
| 28 | + Step |
| 29 | +
|
| 30 | + .. tab-item:: After |
| 31 | + |
| 32 | + .. code:: robotframework |
| 33 | +
|
| 34 | + *** Keywords *** |
| 35 | + Keyword |
| 36 | + [Documentation] |
| 37 | + ... |
| 38 | + ... Arguments: |
| 39 | + ... ${arg}: |
| 40 | + ... |
| 41 | + ... Returns: |
| 42 | + ... ${var} |
| 43 | + ... ${var2} |
| 44 | + [Arguments] ${arg} |
| 45 | + ${var} ${var2} Step |
| 46 | + RETURN ${var} ${var2} |
| 47 | +
|
| 48 | + Keyword With ${embedded} Variable |
| 49 | + [Documentation] |
| 50 | + ... |
| 51 | + ... Arguments: |
| 52 | + ... ${embedded}: |
| 53 | + Step |
| 54 | +
|
| 55 | +Overwriting documentation |
| 56 | +-------------------------- |
| 57 | + |
| 58 | +The documentation will not be added if it is already present in the keyword. You can configure it |
| 59 | +by using ``overwrite`` parameter: |
| 60 | + |
| 61 | +.. tab-set:: |
| 62 | + |
| 63 | + .. tab-item:: Before |
| 64 | + |
| 65 | + .. code:: robotframework |
| 66 | +
|
| 67 | + *** Keywords *** |
| 68 | + Keyword With Documentation |
| 69 | + [Documentation] Short description. |
| 70 | + [Arguments] ${arg} |
| 71 | + Step |
| 72 | +
|
| 73 | + .. tab-item:: After (overwrite = False) |
| 74 | + |
| 75 | + .. code:: robotframework |
| 76 | +
|
| 77 | + *** Keywords *** |
| 78 | + Keyword With Documentation |
| 79 | + [Documentation] Short description. |
| 80 | + [Arguments] ${arg} |
| 81 | + Step |
| 82 | +
|
| 83 | + .. tab-item:: After (overwrite = True) |
| 84 | + |
| 85 | + .. code:: robotframework |
| 86 | +
|
| 87 | + *** Keywords *** |
| 88 | + Keyword With Documentation |
| 89 | + [Documentation] |
| 90 | + ... Arguments: |
| 91 | + ... ${arg}: |
| 92 | + [Arguments] ${arg} |
| 93 | + Step |
| 94 | +
|
| 95 | +Custom template |
| 96 | +---------------- |
| 97 | + |
| 98 | +Custom templates can be loaded from the file using ``doc_template`` parameter. If you pass |
| 99 | +``google`` string it will use default template:: |
| 100 | + |
| 101 | + > robotidy --configure GenerateDocumentation:doc_template=google src |
| 102 | + |
| 103 | +Templates support `Jinja templating engine <https://jinja.palletsprojects.com/>`_ and we are providing several variables |
| 104 | +based on the keyword data. Below, there is a default template:: |
| 105 | + |
| 106 | + {% if keyword.arguments|length > 0 %} |
| 107 | + {{ formatting.cont_indent }}Args: |
| 108 | + {%- for arg in keyword.arguments %} |
| 109 | + {{ formatting.cont_indent }}{{ formatting.cont_indent }}{{ arg.name }}: {% endfor %} |
| 110 | + {% endif -%} |
| 111 | + {% if keyword.returns|length > 0 %} |
| 112 | + {{ formatting.cont_indent }}Returns: |
| 113 | + {%- for value in keyword.returns %} |
| 114 | + {{ formatting.cont_indent }}{{ formatting.cont_indent }}{{ value }}: {% endfor %} |
| 115 | + {% endif -%} |
| 116 | + |
| 117 | +The Jinja syntax is described `here <https://jinja.palletsprojects.com/en/3.1.x/templates/>`_. You can use it as |
| 118 | +a reference to create your own template. The following subsections explain in detail possible features. |
| 119 | + |
| 120 | +Path to template can be absolute or relative (to working directory or configuration file directory):: |
| 121 | + |
| 122 | + > robotidy --configure GenerateDocumentation;doc_template="C:/doc_templates/template.jinja" src |
| 123 | + > robotidy --configure GenerateDocumentation:doc_template="template.jinja" src |
| 124 | + |
| 125 | +Note that to pass parameter value with ``:`` (like a absolute path), you need to use ``;`` as a parameter separator. |
| 126 | + |
| 127 | +.. dropdown:: First line of the documentation |
| 128 | + |
| 129 | + The first line of the template is also the first line of the documentation that goes next to the ``[Documentation]`` setting. |
| 130 | + |
| 131 | + .. tab-set:: |
| 132 | + |
| 133 | + .. tab-item:: Template |
| 134 | + |
| 135 | + .. code:: text |
| 136 | +
|
| 137 | + First line of template |
| 138 | + Second line of example |
| 139 | + Third line. |
| 140 | +
|
| 141 | + .. tab-item:: Generated example |
| 142 | + |
| 143 | + .. code:: robotframework |
| 144 | +
|
| 145 | + *** Keywords *** |
| 146 | + Keyword |
| 147 | + [Documentation] First line of template |
| 148 | + ... Second line of example |
| 149 | + ... Third line. |
| 150 | + Step |
| 151 | +
|
| 152 | + Leave the first line empty in the template if you want to start documentation from the second line. |
| 153 | + |
| 154 | +.. dropdown:: Whitespace can be static or dynamic |
| 155 | + |
| 156 | + Any whitespace in the template will apply to the documentation. For example you can put 4 spaces after every argument |
| 157 | + and before `->` sign: |
| 158 | + |
| 159 | + .. tab-set:: |
| 160 | + |
| 161 | + .. tab-item:: Template |
| 162 | + |
| 163 | + .. code:: text |
| 164 | +
|
| 165 | + Args: |
| 166 | + {%- for arg in keyword.arguments %} |
| 167 | + {{ arg.name }} ->{% endfor %} |
| 168 | +
|
| 169 | + .. tab-item:: Code |
| 170 | + |
| 171 | + .. code:: robotframework |
| 172 | +
|
| 173 | + *** Keywords *** |
| 174 | + Keyword |
| 175 | + [Arguments] ${arguments} ${arg} ${arg2} |
| 176 | + Step |
| 177 | +
|
| 178 | + .. tab-item:: Generated example |
| 179 | + |
| 180 | + .. code:: robotframework |
| 181 | +
|
| 182 | + *** Keywords *** |
| 183 | + Keyword |
| 184 | + [Documentation] |
| 185 | + ... Args: |
| 186 | + ... ${arguments} -> |
| 187 | + ... ${arg} -> |
| 188 | + ... ${arg2} -> |
| 189 | + [Arguments] ${arguments} ${arg} ${arg2} |
| 190 | + Step |
| 191 | +
|
| 192 | + Robotidy provides also ``formatting`` class that contains two variables: |
| 193 | + |
| 194 | + - ``separator`` (default 4 spaces) - spacing between tokens |
| 195 | + - ``cont_indent`` (default 4 spaces) - spacing after ``...`` signs |
| 196 | + |
| 197 | + You can use them in the template - and their values will be affected by your configuration:: |
| 198 | + |
| 199 | + {{ formatting.separator }} |
| 200 | + {{ formatting.cont_indent }} |
| 201 | + |
| 202 | +.. dropdown:: Arguments data |
| 203 | + |
| 204 | + Robotidy provides arguments as a list of variables in ``keyword.arguments`` variable. Every argument contains the following |
| 205 | + variables: |
| 206 | + |
| 207 | + - ``name`` - name of the argument without default value (ie. ``${arg}``) |
| 208 | + - ``default`` - default value (ie. ``value``) |
| 209 | + - ``full_name`` - name with default value (ie. ``${arg} = value``) |
| 210 | + |
| 211 | + You can use them in the template: |
| 212 | + |
| 213 | + .. tab-set:: |
| 214 | + |
| 215 | + .. tab-item:: Template |
| 216 | + |
| 217 | + .. code:: text |
| 218 | +
|
| 219 | + Arguments: |
| 220 | + {%- for arg in keyword.arguments %} |
| 221 | + {{ arg.name }} - {{ arg.default }}:{% endfor %} |
| 222 | +
|
| 223 | + .. tab-item:: Code |
| 224 | + |
| 225 | + .. code:: robotframework |
| 226 | +
|
| 227 | + *** Keywords *** |
| 228 | + Keyword |
| 229 | + [Arguments] ${var} ${var2} = 2 |
| 230 | + Step |
| 231 | +
|
| 232 | + .. tab-item:: Generated example |
| 233 | + |
| 234 | + .. code:: robotframework |
| 235 | +
|
| 236 | + *** Keywords *** |
| 237 | + Keyword |
| 238 | + [Documentation] |
| 239 | + ... Arguments: |
| 240 | + ... ${var} - : |
| 241 | + ... ${var2} - 2: |
| 242 | + [Arguments] ${var} ${var2} = 2 |
| 243 | + Step |
| 244 | +
|
| 245 | + Note that you can use Jinja templating features like ``if`` blocks. For example, if you want to put ``=`` between |
| 246 | + the argument name and default value (only if default value is not empty), you can use:: |
| 247 | + |
| 248 | + {{ arg.name }}{% if arg.default %} = '{{ arg.default }}'{% endif %} |
| 249 | + |
| 250 | +.. dropdown:: Returned values data |
| 251 | + |
| 252 | + Returned values are provided as a list of variables names in ``keyword.returns`` variable. |
| 253 | + |
| 254 | + .. tab-set:: |
| 255 | + |
| 256 | + .. tab-item:: Template |
| 257 | + |
| 258 | + .. code:: text |
| 259 | +
|
| 260 | + Returns: |
| 261 | + {%- for value in keyword.returns %} |
| 262 | + {{ value }}:{% endfor %} |
| 263 | +
|
| 264 | + .. tab-item:: Code |
| 265 | + |
| 266 | + .. code:: robotframework |
| 267 | +
|
| 268 | + *** Keywords *** |
| 269 | + Keyword |
| 270 | + ${value} Step |
| 271 | + RETURN ${value} |
| 272 | +
|
| 273 | + .. tab-item:: Generated example |
| 274 | + |
| 275 | + .. code:: robotframework |
| 276 | +
|
| 277 | + *** Keywords *** |
| 278 | + Keyword |
| 279 | + [Documentation] |
| 280 | + ... Returns: |
| 281 | + ... ${value}: |
| 282 | + ${value} Step |
| 283 | + RETURN ${value} |
| 284 | +
|
| 285 | +.. dropdown:: Keyword name |
| 286 | + |
| 287 | + You can add current keyword name to the documentation using ``keyword.name`` variable. |
| 288 | + |
| 289 | + .. tab-set:: |
| 290 | + |
| 291 | + .. tab-item:: Template |
| 292 | + |
| 293 | + .. code:: text |
| 294 | +
|
| 295 | + This is documentation for '{{ keyword.name }}' keyword. |
| 296 | +
|
| 297 | + .. tab-item:: Code |
| 298 | + |
| 299 | + .. code:: robotframework |
| 300 | +
|
| 301 | + *** Keywords *** |
| 302 | + Keyword |
| 303 | + Step |
| 304 | +
|
| 305 | + Other Keyword |
| 306 | + Step 2 |
| 307 | +
|
| 308 | + .. tab-item:: Generated example |
| 309 | + |
| 310 | + .. code:: robotframework |
| 311 | +
|
| 312 | + *** Keywords *** |
| 313 | + Keyword |
| 314 | + [Documentation] This is documentation for 'Keyword' keyword. |
| 315 | + Step |
| 316 | +
|
| 317 | + Other Keyword |
| 318 | + [Documentation] This is documentation for 'Other Keyword' keyword. |
| 319 | + Step 2 |
0 commit comments