Skip to content

Commit a39acd6

Browse files
committed
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 542bb4e commit a39acd6

6 files changed

Lines changed: 109 additions & 46 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="39-43"
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', 63, 83, remove_indent=True) =]]
193+
[[= include_code('code_samples/recent_activity/src/Command/ActivityLogContextTestCommand.php', 64, 83, 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 translation:extract en -
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: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ 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', 12, 14) =]] // ...
34-
[[= include_file('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 31, 47) =]]
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', 13, 14) =]]
35+
// ...
36+
[[= include_code('code_samples/api/public_php_api/src/Command/FindContentCommand.php', 32, 47) =]]
3537
```
3638

3739
[`SearchService::findContentInfo`](/api/php_api/php_api_reference/classes/Ibexa-Contracts-Core-Repository-SearchService.html#method_findContentInfo) (line 16)
@@ -119,17 +121,19 @@ For example, the following command lists all content items under the specified p
119121

120122
``` php hl_lines="13-16"
121123
// ...
122-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterCommand.php', 4, 9) =]]
124+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterCommand.php', 5, 9) =]]
125+
123126
// ...
124-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterCommand.php', 32, 52) =]]
127+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterCommand.php', 33, 52) =]]
125128
```
126129

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

129132
``` php hl_lines="17"
130133
// ...
131-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 4, 9) =]]// ...
132-
[[= include_file('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 32, 52) =]]
134+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 5, 9) =]]
135+
// ...
136+
[[= include_code('code_samples/api/public_php_api/src/Command/FilterLocationCommand.php', 33, 52) =]]
133137
```
134138

135139
!!! caution
@@ -181,8 +185,9 @@ For example, in the code below, `locationId` is provided to list all children of
181185

182186
``` php hl_lines="22-24"
183187
// ...
184-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomController.php', 4, 12) =]] // ...
185-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomController.php', 19, 35) =]]
188+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomController.php', 5, 12) =]]
189+
// ...
190+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomController.php', 20, 35) =]]
186191
```
187192

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

192197
``` php hl_lines="19"
193198
// ...
194-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 4, 12) =]] // ...
195-
[[= include_file('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 19, 34) =]]
199+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 5, 12) =]]
200+
// ...
201+
[[= include_code('code_samples/api/public_php_api/src/Controller/CustomFilterController.php', 20, 34) =]]
196202
```
197203

198204
### Paginate search results
@@ -201,15 +207,15 @@ To paginate search or filtering results, it's recommended to use the [Pagerfanta
201207

202208
``` php
203209
// ...
204-
[[= include_file('code_samples/api/public_php_api/src/Controller/PaginationController.php', 8, 15) =]] // ...
205-
[[= include_file('code_samples/api/public_php_api/src/Controller/PaginationController.php', 22, 32) =]]
206-
[[= include_file('code_samples/api/public_php_api/src/Controller/PaginationController.php', 33, 43) =]]
210+
[[= include_code('code_samples/api/public_php_api/src/Controller/PaginationController.php', 9, 15) =]]
211+
// ...
212+
[[= include_code('code_samples/api/public_php_api/src/Controller/PaginationController.php', 23, 43) =]]
207213
```
208214

209215
Pagination can then be rendered for example using the following template:
210216

211217
``` html+twig
212-
[[= include_file('code_samples/api/public_php_api/templates/themes/standard/full/custom_pagination.html.twig') =]]
218+
[[= include_code('code_samples/api/public_php_api/templates/themes/standard/full/custom_pagination.html.twig') =]]
213219
```
214220

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

241247
``` php
242-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 43, 49) =]][[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 53, 54) =]]
243-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 60, 65) =]]
248+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 45, 49) =]]
249+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 54, 54) =]]
250+
251+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 61, 65) =]]
244252
```
245253

246254
This example takes three parameters from a command — `$text`, `$contentTypeId`, and `$locationId`.
@@ -254,7 +262,8 @@ The example below uses the `LogicalNot` operator to search for all content conta
254262
that doesn't belong to the provided Section:
255263

256264
``` php
257-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 45, 46) =]][[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 48, 53) =]]
265+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 46, 46) =]]
266+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 49, 53) =]]
258267
```
259268

260269
### Combine independent Criteria
@@ -287,7 +296,7 @@ To sort the results of a query, use one of more [Sort Clauses](sort_clause_refer
287296
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:
288297

289298
``` php
290-
[[= include_file('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 55, 59) =]]
299+
[[= include_code('code_samples/api/public_php_api/src/Command/FindComplexCommand.php', 56, 59) =]]
291300
```
292301

293302
!!! tip
@@ -305,21 +314,21 @@ With aggregations you can find the count of search results or other result infor
305314
To do this, you use of the query's `$aggregations` property:
306315

307316
``` php
308-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 34, 39) =]]
317+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 35, 39) =]]
309318
```
310319

311320
The name of the aggregation must be unique in the given query.
312321

313322
Access the results by using the `get()` method of the aggregation:
314323

315324
``` php
316-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 43, 44) =]]
325+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 44, 44) =]]
317326
```
318327

319328
Aggregation results contain the name of the result and the count of found items:
320329

321330
``` php
322-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 46, 49) =]]
331+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 47, 49) =]]
323332
```
324333

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

330339
``` php
331-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 39, 40) =]]
340+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 40, 40) =]]
332341
```
333342

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

337346
``` php
338-
[[= include_file('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 34, 37) =]]
347+
[[= include_code('code_samples/api/public_php_api/src/Command/FindWithAggregationCommand.php', 35, 37) =]]
339348
```
340349

341350
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.
@@ -390,7 +399,7 @@ You build an `EmbeddingQuery` instance by using a builder and pass it to the sea
390399
This example shows a minimal embedding query executed directly through the search service:
391400

392401
``` php hl_lines="39-40 42-48 50"
393-
[[= include_file('code_samples/api/public_php_api/src/Command/FindByTaxonomyEmbeddingCommand.php') =]]
402+
[[= include_code('code_samples/api/public_php_api/src/Command/FindByTaxonomyEmbeddingCommand.php') =]]
394403
```
395404

396405
For more information, see [Embeddings reference](embeddings_reference.md).
@@ -407,8 +416,9 @@ For a list of supported Criteria and Sort Clauses, see [Search in trash referenc
407416
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.
408417

409418
``` php
410-
[[= include_file('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 4, 6) =]]//...
411-
[[= include_file('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 35, 42) =]]
419+
[[= include_code('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 5, 6) =]]
420+
//...
421+
[[= include_code('code_samples/api/public_php_api/src/Command/FindInTrashCommand.php', 36, 42) =]]
412422
```
413423

414424
!!! 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
@@ -31,6 +31,7 @@ def define_env(env):
3131
@env.macro
3232
def include_file(filename, start_line=0, end_line=None, glue='', remove_indent=False):
3333
"""
34+
DEPRECATED: Use include_code instead.
3435
Include a file,
3536
optionally indicating start_line and end_line (start counting from 0)
3637
optionally set a glue string to lead every string except the first one (can be used for indent)
@@ -51,6 +52,18 @@ def include_file(filename, start_line=0, end_line=None, glue='', remove_indent=F
5152

5253
return glue.join(line_range)
5354

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

0 commit comments

Comments
 (0)