Skip to content

Commit afc1bc3

Browse files
authored
Ease code inclusion (#3169)
* main.py: Add include_code function * code_samples_usage.php: Update for include_code * convert docs/administration/recent_activity/recent_activity.md * convert docs/api/rest_api/rest_api_usage/rest_requests.md * convert docs/search/search_api.md * convert docs/templating/twig_function_reference/image_twig_functions.md
1 parent 8d1b240 commit afc1bc3

6 files changed

Lines changed: 112 additions & 48 deletions

File tree

docs/administration/recent_activity/recent_activity.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ In the following example, log groups that contain at least one creation of a Con
8383
It uses the default `admin` user that has a [permission](#permission-and-security) to list everyone's entries.
8484

8585
```php hl_lines="34-38"
86-
[[= include_file('code_samples/recent_activity/src/Command/MonitorRecentContentCreationCommand.php') =]]
86+
[[= include_code('code_samples/recent_activity/src/Command/MonitorRecentContentCreationCommand.php') =]]
8787
```
8888

8989
```console
@@ -143,7 +143,7 @@ In the following example, an event subscriber is subscribing to an event dispatc
143143
This event has the information needed by a log entry (see details after the example).
144144

145145
```php
146-
[[= include_file('code_samples/recent_activity/src/EventSubscriber/MyFeatureEventSubscriber.php') =]]
146+
[[= include_code('code_samples/recent_activity/src/EventSubscriber/MyFeatureEventSubscriber.php') =]]
147147
```
148148

149149
`ActivityLogService::build()` function returns an `Ibexa\Contracts\ActivityLog\Values\CreateActivityLogStruct` which can then be passed to `ActivityLogService::save`.
@@ -190,7 +190,7 @@ In the following example, several actions are logged into one context group, eve
190190
- `complete`
191191

192192
``` php
193-
[[= include_file('code_samples/recent_activity/src/Command/ActivityLogContextTestCommand.php', 46, 66, remove_indent=True) =]]
193+
[[= include_code('code_samples/recent_activity/src/Command/ActivityLogContextTestCommand.php', 47, 66, remove_indent=True) =]]
194194
```
195195

196196
Context groups can't be nested.
@@ -227,13 +227,13 @@ First, follow an example of a default template overriding the one from the bundl
227227
It can be used during development as a fallback for classes that aren't mapped yet.
228228

229229
``` twig
230-
[[= include_file('code_samples/recent_activity/templates/themes/admin/activity_log/ui/default.html.twig') =]]
230+
[[= include_code('code_samples/recent_activity/templates/themes/admin/activity_log/ui/default.html.twig') =]]
231231
```
232232

233233
Here is an example of a `ClassNameMapperInterface` associating the class `App\MyFeature\MyFeature` with the identifier `my_feature`:
234234

235235
``` php
236-
[[= include_file('code_samples/recent_activity/src/ActivityLog/ClassNameMapper/MyFeatureNameMapper.php') =]]
236+
[[= include_code('code_samples/recent_activity/src/ActivityLog/ClassNameMapper/MyFeatureNameMapper.php') =]]
237237
```
238238

239239
This mapper also provides a translation for the class name in the **Filters** menu.
@@ -242,21 +242,21 @@ This translation can be extracted with `php bin/console jms:translation:extract
242242
To be taken into account, this mapper must be registered as a service:
243243

244244
``` yaml
245-
[[= include_file('code_samples/recent_activity/config/append_to_services.yaml') =]]
245+
[[= include_code('code_samples/recent_activity/config/append_to_services.yaml') =]]
246246
```
247247

248248
Here is an example of a `PostActivityListLoadEvent` subscriber which loads the related object when it's an `App\MyFeature\MyFeature`, and attaches it to the log entry:
249249

250250
``` php
251-
[[= include_file('code_samples/recent_activity/src/EventSubscriber/MyFeaturePostActivityListLoadEventSubscriber.php') =]]
251+
[[= include_code('code_samples/recent_activity/src/EventSubscriber/MyFeaturePostActivityListLoadEventSubscriber.php') =]]
252252
```
253253

254254
The following template is made to display the object of `App\MyFeature\MyFeature` (now identified as `my_feature`) when the action is `simulate`,
255255
so, it's named in `templates/themes/admin/activity_log/ui/my_feature/simulate.html.twig`.
256256
Thanks to the previous subscriber, the related object is available at display time:
257257

258258
``` twig
259-
[[= include_file('code_samples/recent_activity/templates/themes/admin/activity_log/ui/my_feature/simulate.html.twig') =]]
259+
[[= include_code('code_samples/recent_activity/templates/themes/admin/activity_log/ui/my_feature/simulate.html.twig') =]]
260260
```
261261

262262
## REST API

docs/api/rest_api/rest_api_usage/rest_requests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,13 @@ This script:
169169
=== "XML"
170170

171171
``` php
172-
[[= include_file('code_samples/api/rest_api/create_image.xml.php', 0, None, ' ') =]]
172+
[[= include_code('code_samples/api/rest_api/create_image.xml.php', 1, None, 1) =]]
173173
```
174174

175175
=== "JSON"
176176

177177
``` php
178-
[[= include_file('code_samples/api/rest_api/create_image.json.php', 0, None, ' ') =]]
178+
[[= include_code('code_samples/api/rest_api/create_image.json.php', indent_level=1) =]]
179179
```
180180

181181
### Search (`/views`)

docs/search/search_api.md

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ The following command takes the content type identifier as an argument and lists
2929

3030
``` php hl_lines="14 16"
3131
// ...
32-
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 4, 7) =]]// ...
33-
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 17, 19) =]] // ...
34-
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 32, 48) =]]
35-
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 48, 49) =]]
32+
[[= include_code('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 5, 7) =]]
33+
// ...
34+
[[= include_code('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 18, 19) =]]
35+
// ...
36+
[[= include_code('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 33, 48) =]]
37+
[[= include_code('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 49, 49) =]]
3638
```
3739

3840
[`SearchService::findContentInfo`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-SearchService.html#method_findContentInfo) (line 16)
@@ -120,18 +122,22 @@ For example, the following command lists all content items under the specified p
120122

121123
``` php hl_lines="15-18"
122124
// ...
123-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterCommand.php', 4, 9) =]]
124-
// ...
125-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterCommand.php', 19, 21) =]][[= include_file('code_samples/api/public_php_api/src/Command/FilterCommand.php', 33, 53) =]]
125+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterCommand.php', 5, 9) =]]
126+
127+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterCommand.php', 20, 21) =]]
128+
// ...
129+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterCommand.php', 34, 53) =]]
126130
```
127131

128132
The same Filter can be applied to find locations instead of content items, for example:
129133

130134
``` php hl_lines="20"
131135
// ...
132-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 4, 9) =]]
133-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterCommand.php', 19, 21) =]]// ...
134-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 33, 53) =]]
136+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 5, 9) =]]
137+
138+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterCommand.php', 20, 21) =]]
139+
// ...
140+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 34, 53) =]]
135141
```
136142

137143
!!! caution
@@ -183,8 +189,9 @@ For example, in the code below, `locationId` is provided to list all children of
183189

184190
``` php hl_lines="22-24"
185191
// ...
186-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomController.php', 4, 12) =]] // ...
187-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomController.php', 16, 32) =]]
192+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomController.php', 5, 12) =]]
193+
// ...
194+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomController.php', 17, 32) =]]
188195
```
189196

190197
The rendering of results is then relegated to [templates](templates.md) (lines 22-24).
@@ -193,8 +200,9 @@ When using Repository filtering, provide the results of `ContentService::find()`
193200

194201
``` php hl_lines="19"
195202
// ...
196-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 4, 12) =]] // ...
197-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 16, 31) =]]
203+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 5, 12) =]]
204+
// ...
205+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 17, 31) =]]
198206
```
199207

200208
### Paginate search results
@@ -203,14 +211,15 @@ To paginate search or filtering results, it's recommended to use the [Pagerfanta
203211

204212
``` php
205213
// ...
206-
[[= include_file('code_samples/api/public_php_api/src/Controller/PaginationController.php', 8, 15) =]] // ...
207-
[[= include_file('code_samples/api/public_php_api/src/Controller/PaginationController.php', 19, 39) =]]
214+
[[= include_code('code_samples/api/public_php_api/src/Controller/PaginationController.php', 9, 15) =]]
215+
// ...
216+
[[= include_code('code_samples/api/public_php_api/src/Controller/PaginationController.php', 20, 39) =]]
208217
```
209218

210219
Pagination can then be rendered for example using the following template:
211220

212221
``` html+twig
213-
[[= include_file('code_samples/api/public_php_api/templates/themes/standard/full/custom_pagination.html.twig') =]]
222+
[[= include_code('code_samples/api/public_php_api/templates/themes/standard/full/custom_pagination.html.twig') =]]
214223
```
215224

216225
For more information and examples, see [PagerFanta documentation](https://www.babdev.com/open-source/packages/pagerfanta/docs/2.x/usage).
@@ -241,9 +250,10 @@ For more complex searches, you need to combine multiple Criteria.
241250
You can do it using logical operators: `LogicalAnd`, `LogicalOr`, and `LogicalNot`.
242251

243252
``` php
244-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 44, 49) =]][[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 53, 54) =]]
245-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 60, 65) =]]
253+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 45, 49) =]]
254+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 54, 54) =]]
246255

256+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 61, 65) =]]
247257
```
248258

249259
This example takes three parameters from a command — `$text`, `$contentTypeId`, and `$locationId`.
@@ -257,7 +267,7 @@ The example below uses the `LogicalNot` operator to search for all content conta
257267
that doesn't belong to the provided Section:
258268

259269
``` php
260-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 46, 54) =]]
270+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 47, 54) =]]
261271
```
262272

263273
### Combine independent Criteria
@@ -290,7 +300,7 @@ To sort the results of a query, use one of more [Sort Clauses](sort_clause_refer
290300
For example, to order search results by their publication date, from oldest to newest, and then alphabetically by content name, add the following Sort Clauses to the query:
291301

292302
``` php
293-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 55, 59) =]]
303+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 56, 59) =]]
294304
```
295305

296306
!!! tip
@@ -308,21 +318,21 @@ With aggregations you can find the count of search results or other result infor
308318
To do this, you use of the query's `$aggregations` property:
309319

310320
``` php
311-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 30, 35) =]]
321+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 31, 35) =]]
312322
```
313323

314324
The name of the aggregation must be unique in the given query.
315325

316326
Access the results by using the `get()` method of the aggregation:
317327

318328
``` php
319-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 39, 40) =]]
329+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 40, 40) =]]
320330
```
321331

322332
Aggregation results contain the name of the result and the count of found items:
323333

324334
``` php
325-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 42, 45) =]]
335+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 43, 45) =]]
326336
```
327337

328338
With field aggregations you can group search results according to the value of a specific field.
@@ -331,14 +341,14 @@ In this case the aggregation takes the content type identifier and the field ide
331341
The following example creates an aggregation named `selection` that groups results according to the value of the `topic` field in the `article` content type:
332342

333343
``` php
334-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 35, 36) =]]
344+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 36, 36) =]]
335345
```
336346

337347
With term aggregation you can define additional limits to the results.
338348
The following example limits the number of terms returned to 5 and only considers terms that have 10 or more results:
339349

340350
``` php
341-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 30, 33) =]]
351+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 31, 33) =]]
342352
```
343353

344354
To use a range aggregation, you must provide a `ranges` array containing a set of `Range` objects that define the borders of the specific range sets.
@@ -394,7 +404,7 @@ You build an `EmbeddingQuery` instance by using a builder and pass it to the sea
394404
This example shows a minimal embedding query executed directly through the search service:
395405

396406
``` php hl_lines="38-39 41-47 49"
397-
[[= include_file('code_samples/api/public_php_api/src/Command/FindByTaxonomyEmbeddingCommand.php') =]]
407+
[[= include_code('code_samples/api/public_php_api/src/Command/FindByTaxonomyEmbeddingCommand.php') =]]
398408
```
399409

400410
For more information, see [Embeddings reference](embeddings_reference.md).
@@ -411,8 +421,9 @@ For a list of supported Criteria and Sort Clauses, see [Search in trash referenc
411421
Searching through the trashed content items operates directly on the database, therefore you cannot use external search engines, such as Solr or Elasticsearch, and it's impossible to reindex the data.
412422

413423
``` php
414-
[[= include_file('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 4, 6) =]]//...
415-
[[= include_file('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 35, 42) =]]
424+
[[= include_code('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 5, 6) =]]
425+
//...
426+
[[= include_code('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 36, 42, remove_indent=True) =]]
416427
```
417428

418429
!!! caution

docs/templating/twig_function_reference/image_twig_functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ page_type: reference
1313
To render images, use the [`ibexa_render_field()`](field_twig_functions.md#ibexa_render_field) Twig function with the variation name passed as an argument, for example:
1414

1515
``` html+twig
16-
[[= include_file('docs/templating/twig_function_reference/field_twig_functions.md', 40, 48) =]]
16+
[[= include_code('docs/templating/twig_function_reference/field_twig_functions.md', 42, 48) =]]
1717
```
1818

1919
## Image information

main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def define_env(env):
3232
@env.macro
3333
def include_file(filename, start_line=0, end_line=None, glue='', remove_indent=False):
3434
"""
35+
DEPRECATED: Use include_code instead.
3536
Include a file,
3637
optionally indicating start_line and end_line (start counting from 0)
3738
optionally set a glue string to lead every string except the first one (can be used for indent)
@@ -52,6 +53,18 @@ def include_file(filename, start_line=0, end_line=None, glue='', remove_indent=F
5253

5354
return glue.join(line_range)
5455

56+
@env.macro
57+
def include_code(file_path, start_line=1, end_line=None, indent_level=0, remove_indent=False):
58+
"""
59+
Include a file
60+
file_path (string): The path to the file from project root
61+
start_line (int): The line number to start including from (start counting from 1) - default is 1 (include first line)
62+
end_line (int or None): The line number to end including to. If None, include until the end of the file - default is None (include end of file)
63+
indent_level (int): The number of indent (4 spaces) to add to the beginning of each line - default is 0 (no indent added).
64+
remove_indent (bool): Whether to remove absolute indent, the maximum of leading whitespaces without breaking relative indent - default is False (no indent removed)
65+
"""
66+
return include_file(file_path, start_line-1, end_line, ' ' * indent_level, remove_indent).rstrip()
67+
5568
@env.macro
5669
def cards(pages, columns=1, style="cards", force_version=False):
5770
current_page = env.variables.page

0 commit comments

Comments
 (0)