Skip to contentSkip to conversation

doc/contributing/documentation: add info about including code

PATCH Superseded Conor Walsh 6 participants 20 messages Apr 21, 2021 – May 19, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 doc/contributing/documentation: add info about including code

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.

---
 doc/guides/contributing/documentation.rst | 45 +++++++++++++++++++++++
 1 file changed, 45 insertions(+)
doc/guides/contributing/documentation.rst +45 -0
index a4e6be6aca..d5dd119a9a 100644
@@ -433,6 +433,51 @@ Code and Literal block sections
          return 0;
       }
 
+* Code snippets can also be included directly from the code using the ``literalinclude`` block.
+  Using this block instead of a code block will ensure that the code snippets shown in the
+  documentation are always up to date with the code.
+
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1
+
+  This would be rendered as:
+
+  .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+    :language: c
+    :start-after: Display the port MAC address.
+    :end-before: Enable RX in promiscuous mode for the Ethernet device.
+    :dedent: 1
+
+  Specifying ``:language:`` will enable syntax highlighting for the specified language.
+  ``:dedent:`` is used in this example to remove 1 leading tab from each line of the snippet.
+
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */
+
+  ``...`` could then be included in the docs using::
+
+      .. literalinclude:: ../../../examples/sample_app/main.c
+        :language: c
+        :start-after: #guide_doc: Example feature being documented.
+        :end-before: #guide_doc: End of example feature being documented.
+
+* More information about the ``literalinclude`` block can be found within the
+  `Sphinx Documentation <https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html?highlight=literalinclude#directive-literalinclude>`_.
 
 * The default encoding for a literal block using the simplified ``::``
   directive is ``none``.
Signature · 1 line
2.25.1
Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines

-----Original Message-----
From: Walsh, Conor <[email protected]>
Sent: Wednesday, April 21, 2021 10:12 AM
To: Mcnamara, John <[email protected]>; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; Richardson, Bruce <[email protected]>; Burakov, Anatoly <[email protected]>
Cc: [email protected]; Walsh, Conor <[email protected]>
Subject: [PATCH] doc/contributing/documentation: add info about including code

Quoted 8 lines

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.

This is a very good suggestion and I think we should encourage doc writers to use this when including code in the documentation.

#2 lore raw

21/04/2021 12:21, Mcnamara, John:

Quoted 10 lines

From: Walsh, Conor <[email protected]>

Quoted 8 lines

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.

Quoted 2 lines

This is a very good suggestion and I think we should encourage doc writers to use this when including code in the documentation.

Yes, and we should try to clean-up existing code snippets.

#3 lore raw
Quoted 12 lines

-----Original Message-----
From: Thomas Monjalon <[email protected]>
Sent: Wednesday, April 21, 2021 11:31 AM
To: Walsh, Conor <[email protected]>; [email protected]; Yigit, Ferruh <[email protected]>; Richardson, Bruce <[email protected]>; Burakov, Anatoly <[email protected]>; Mcnamara, John <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] doc/contributing/documentation: add info about including code

Quoted 1 line
Quoted 4 lines
Quoted 2 lines

This is a very good suggestion and I think we should encourage doc writers to use this when including code in the documentation.

Yes, and we should try to clean-up existing code snippets.

If there is no general objection to including code snippets this way then we could get someone to work on refactoring the sample code docs to use it.

John

#4 lore raw

21/04/2021 16:17, Mcnamara, John:

Quoted 9 lines

From: Thomas Monjalon <[email protected]>

Quoted 5 lines
Quoted 3 lines

This is a very good suggestion and I think we should encourage doc writers to use this when including code in the documentation.

Yes, and we should try to clean-up existing code snippets.

If there is no general objection to including code snippets this way then we could get someone to work on refactoring the sample code docs to use it.

I'd love such rework!
Removing lines is always good :)

#5 lore raw
Quoted 7 lines · Thomas Monjalon

21/04/2021 16:17, Mcnamara, John:

Quoted 5 lines

From: Thomas Monjalon <[email protected]>

Quoted 4 lines
Quoted 3 lines

This is a very good suggestion and I think we should encourage doc writers to use this when including code in the documentation.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Yes, and we should try to clean-up existing code snippets.

If there is no general objection to including code snippets this way then we could get someone to work on refactoring the sample code docs to use it.

I'd love such rework!
Removing lines is always good :)

+1 :-)

--
David Marchand

Acked-by on v1 1/1
#6 lore raw
In reply to Conor Walsh
Quoted 9 lines · Conor Walsh

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.

Note: We used literalinclude in the past and there was an issue with pdf:
https://git.dpdk.org/dpdk/commit/?id=d3ce1dc637c1bbef9a407f10281b2bc0549256da
But we don't generate pdf anymore, so we are good.

Quoted 2 lines
Signature · 1 line
David Marchand
#7 lore raw
In reply to Conor Walsh

21/04/2021 11:11, Conor Walsh:

doc/guides/contributing/documentation.rst Code and Literal block sections · line 446

Quoted 7 lines
doc/guides/contributing/documentation.rst +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

doc/guides/contributing/documentation.rst Code and Literal block sections · line 470

Quoted 12 lines
doc/guides/contributing/documentation.rst +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

#8 lore raw
Quoted 51 lines · Thomas Monjalon

21/04/2021 11:11, Conor Walsh:

doc/guides/contributing/documentation.rst Code and Literal block sections · line 446

Quoted 7 lines
doc/guides/contributing/documentation.rst +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

doc/guides/contributing/documentation.rst Code and Literal block sections · line 470

Quoted 12 lines
doc/guides/contributing/documentation.rst +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 12 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

Signature · 2 lines
Thanks,
Anatoly
#9 lore raw

04/05/2021 11:32, Burakov, Anatoly:

Quoted 66 lines · Thomas Monjalon
Quoted 51 lines

21/04/2021 11:11, Conor Walsh:

doc/guides/contributing/documentation.rst Code and Literal block sections · line 446

Quoted 7 lines
doc/guides/contributing/documentation.rst +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

doc/guides/contributing/documentation.rst Code and Literal block sections · line 470

Quoted 12 lines
doc/guides/contributing/documentation.rst +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 8 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Quoted 5 lines

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

#10 lore raw
Quoted 67 lines · Thomas Monjalon

04/05/2021 11:32, Burakov, Anatoly:

Quoted 66 lines · Thomas Monjalon
Quoted 51 lines

21/04/2021 11:11, Conor Walsh:

doc/guides/contributing/documentation.rst Code and Literal block sections · line 446

Quoted 7 lines
doc/guides/contributing/documentation.rst +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

doc/guides/contributing/documentation.rst Code and Literal block sections · line 470

Quoted 12 lines
doc/guides/contributing/documentation.rst +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 8 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Quoted 10 lines
Quoted 5 lines

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

+1 to Anatoly's comment, to make it obvious to the reader of the code that the comment is used for documentation purposes and use explicit syntax for it.

#11 lore raw

04/05/2021 12:35, Ferruh Yigit:

Quoted 74 lines · Thomas Monjalon
Quoted 73 lines

04/05/2021 11:32, Burakov, Anatoly:

Quoted 71 lines · Thomas Monjalon
Quoted 51 lines

21/04/2021 11:11, Conor Walsh:

doc/guides/contributing/documentation.rst Code and Literal block sections · line 446

Quoted 7 lines
doc/guides/contributing/documentation.rst +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

doc/guides/contributing/documentation.rst Code and Literal block sections · line 470

Quoted 12 lines
doc/guides/contributing/documentation.rst +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 8 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

Quoted 5 lines
Quoted 2 lines

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

+1 to Anatoly's comment, to make it obvious to the reader of the code that the comment is used for documentation purposes and use explicit syntax for it.

So you assume the comment is only for doc extraction? I think it can be a real comment, otherwise we'll need to have 2 lines: 1 for doc extraction, 1 for code comment.

#12 lore raw
Quoted 78 lines · Thomas Monjalon

04/05/2021 12:35, Ferruh Yigit:

Quoted 77 lines · Thomas Monjalon
Quoted 75 lines

04/05/2021 11:32, Burakov, Anatoly:

Quoted 66 lines · Thomas Monjalon
Quoted 51 lines

21/04/2021 11:11, Conor Walsh:

doc/guides/contributing/documentation.rst Code and Literal block sections · line 446

Quoted 7 lines
doc/guides/contributing/documentation.rst +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

doc/guides/contributing/documentation.rst Code and Literal block sections · line 470

Quoted 12 lines
doc/guides/contributing/documentation.rst +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 8 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Quoted 5 lines

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

Quoted 7 lines
Quoted 2 lines

+1 to Anatoly's comment, to make it obvious to the reader of the code that the comment is used for documentation purposes and use explicit syntax for it.

So you assume the comment is only for doc extraction? I think it can be a real comment, otherwise we'll need to have 2 lines: 1 for doc extraction, 1 for code comment.

I see your point, for the cases that there is already a comment before (or after) the code, will it be too bad to have multiple lines, something like:

/* Actual comment
* More details
*
* explicit marker */

I think explicit marker has the advantage of:
- Whoever updating the comment will know that it is a marker for the documentation and be careful on update
- Whoever updating the code between the markers, know that it may be required to re-visit the relevant documentation and update it because of code change
- Whoever reading the code will know that part is in a documentation, and may be interested to go and check the relevant documentation
- Whoever reading the code, and not very familiar with DPDK convention, still can understand what that comment is for and benefit from above

#13 lore raw

04/05/2021 13:15, Ferruh Yigit:

Quoted 96 lines · Thomas Monjalon
Quoted 78 lines

04/05/2021 12:35, Ferruh Yigit:

Quoted 77 lines · Thomas Monjalon
Quoted 75 lines

04/05/2021 11:32, Burakov, Anatoly:

Quoted 66 lines · Thomas Monjalon
Quoted 51 lines

21/04/2021 11:11, Conor Walsh:

Review

Quoted 7 lines
(quoted diff) +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

Review

Quoted 12 lines
(quoted diff) +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 8 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Quoted 5 lines

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

Quoted 7 lines
Quoted 2 lines

+1 to Anatoly's comment, to make it obvious to the reader of the code that the comment is used for documentation purposes and use explicit syntax for it.

So you assume the comment is only for doc extraction? I think it can be a real comment, otherwise we'll need to have 2 lines: 1 for doc extraction, 1 for code comment.

I see your point, for the cases that there is already a comment before (or after) the code, will it be too bad to have multiple lines, something like:

/* Actual comment
* More details
*
* explicit marker */

Quoted 9 lines

I think explicit marker has the advantage of:
- Whoever updating the comment will know that it is a marker for the documentation and be careful on update
- Whoever updating the code between the markers, know that it may be required to re-visit the relevant documentation and update it because of code change
- Whoever reading the code will know that part is in a documentation, and may be interested to go and check the relevant documentation
- Whoever reading the code, and not very familiar with DPDK convention, still can understand what that comment is for and benefit from above

I understand these points.
But I'm afraid the proposed syntax #guide_doc:
is not so obvious for everybody.

I'm sure there can be a better syntax.

#14 lore raw
Quoted 107 lines · Thomas Monjalon

04/05/2021 13:15, Ferruh Yigit:

Quoted 96 lines · Thomas Monjalon
Quoted 78 lines

04/05/2021 12:35, Ferruh Yigit:

Quoted 77 lines · Thomas Monjalon
Quoted 75 lines

04/05/2021 11:32, Burakov, Anatoly:

Quoted 66 lines · Thomas Monjalon
Quoted 51 lines

21/04/2021 11:11, Conor Walsh:

Review

Quoted 7 lines
(quoted diff) +7 -0
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the Ethernet device.
+        :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

[...]

Review

Quoted 12 lines
(quoted diff) +12 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

/* Foo bar >8 */
foo(bar);
/* 8< End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

/*~ Foo bar */
foo(bar);
/*~ End of foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it natural:

Quoted 8 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

Not having markup will 1) risk people accidentally "fixing" or otherwise modifying comments, and 2) has bigger potential for collisions elsewhere in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker docs.

Quoted 5 lines

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example), however i don't think it's a good idea to leave things implicit like your last suggestion.

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

Quoted 7 lines
Quoted 2 lines

+1 to Anatoly's comment, to make it obvious to the reader of the code that the comment is used for documentation purposes and use explicit syntax for it.

So you assume the comment is only for doc extraction? I think it can be a real comment, otherwise we'll need to have 2 lines: 1 for doc extraction, 1 for code comment.

I see your point, for the cases that there is already a comment before (or after) the code, will it be too bad to have multiple lines, something like:

/* Actual comment
* More details
*
* explicit marker */

Quoted 9 lines

I think explicit marker has the advantage of:
- Whoever updating the comment will know that it is a marker for the documentation and be careful on update
- Whoever updating the code between the markers, know that it may be required to re-visit the relevant documentation and update it because of code change
- Whoever reading the code will know that part is in a documentation, and may be interested to go and check the relevant documentation
- Whoever reading the code, and not very familiar with DPDK convention, still can understand what that comment is for and benefit from above

Quoted 6 lines

I understand these points.
But I'm afraid the proposed syntax #guide_doc:
is not so obvious for everybody.

I'm sure there can be a better syntax.

I'm not particularly attached to either syntax, as long as it's clearly documented and explicitly visible in the code (as opposed to something that kinda-sorta-but-doesn't-really-look-like-a-marker type syntax).

So, we'd have to give up some "natural-ness", as *that's the point* of having a syntax. And i'm certainly OK with comment style like this:

/*
* some meaningful comment.
* :doc_marker:
*/
...
/* :end doc_marker: */

Or something to that effect. What i'm *not* in favor of is these markers looking like part of a comment, rather than markers for doxygen.

Signature · 2 lines
Thanks,
Anatoly
#15 lore raw

<snip>

Quoted 13 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

21/04/2021 11:11, Conor Walsh:

Quoted 1 line

+ The following will include a snippet from the skeleton sample

app::

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+        :language: c
+        :start-after: Display the port MAC address.
+        :end-before: Enable RX in promiscuous mode for the

Ethernet device.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+ :dedent: 1

I would prefer indenting the options with 3 spaces to make them aligned with literalinclude.

I will update this in v2.

Quoted 25 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

[...]

Quoted 1 line

+* ``start-after`` and ``end-before`` can use any text within a given

file,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ however it may be difficult to find unique text within your code

to mark the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ start and end of your snippets. In these cases, it is

recommended to include

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+ explicit tags in your code to denote these locations for

documentation purposes.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* #guide_doc: Example feature being documented. */
+    ...
+    /* #guide_doc: End of example feature being documented. */

I think we can standardize this usage in a beautiful syntax. My proposal, using the scissor sign:

      /* Foo bar >8 */
      foo(bar);
      /* 8< End of foo bar */

I like the scissors syntax, I think its lightweight, clear and will help provide a unique string to search for. I would switch the direction of the scissors so that the snippet is between the cuts to make it a bit clearer. /* Foo bar 8< */
foo(bar);
/* >8 End of foo bar */

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar >8
:end-before: 8< End of foo bar

Another idea:

      /*~ Foo bar */
      foo(bar);
      /*~ End of foo bar */
Quoted 16 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

.. literalinclude:: foobar.c
:language: C
:start-after: ~ Foo bar
:end-before: ~ End of foo bar

Maybe we don't need any markup for the start line and keep it

natural:

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

/* Foo bar */
foo(bar);
/* end: Foo bar */

.. literalinclude:: foobar.c
:language: C
:start-after: Foo bar
:end-before: end: Foo bar

I think it definitely needs some syntax so that people realise it isn’t just a normal comment.

Quoted 16 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Not having markup will 1) risk people accidentally "fixing" or

otherwise

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

modifying comments, and 2) has bigger potential for collisions

elsewhere

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

in the comments. While these aren't big risks, IMO it should be explicitly obvious that a comment is not just a comment but a marker

docs.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Having named tags like in the original proposal is the most explicit version of the above, which is why i favor it, but i think it's OK to have a lighter-weight syntax (e.g. with scissors for example),

however i

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

don't think it's a good idea to leave things implicit like your last suggestion.

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 2 lines

I think the first comment is not only for code extraction,
but also for code reader, that's why I think it's good to keep it natural.

+1 to Anatoly's comment, to make it obvious to the reader of the code

that the

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

comment is used for documentation purposes and use explicit syntax

for it.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

So you assume the comment is only for doc extraction?

I think the comments added as part of this should be meaningful comments that include the scissors syntax.
If there is already a multiline comment present then the last line should be the name of the feature or something short with the scissors syntax.

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

I think it can be a real comment, otherwise we'll need to have 2 lines: 1 for doc extraction, 1 for code comment.

I see your point, for the cases that there is already a comment before (or after) the code, will it be too bad to have multiple lines, something like:

/* Actual comment
* More details
*
* explicit marker */

I can add an example that cover this case in v2.
/* Comments
* More comments
*
* Foo Bar 8< */
foo(bar);
/* >8 End of foo bar */

Quoted 36 lines
Quoted 6 lines
Quoted 6 lines

I think explicit marker has the advantage of:
- Whoever updating the comment will know that it is a marker for the documentation and be careful on update
- Whoever updating the code between the markers, know that it may be

required to

Quoted 2 lines
Quoted 2 lines

re-visit the relevant documentation and update it because of code change
- Whoever reading the code will know that part is in a documentation, and

may be

Quoted 2 lines
Quoted 2 lines

interested to go and check the relevant documentation
- Whoever reading the code, and not very familiar with DPDK convention,

still

Quoted 8 lines
Quoted 1 line

can understand what that comment is for and benefit from above

I understand these points.
But I'm afraid the proposed syntax #guide_doc:
is not so obvious for everybody.

I'm sure there can be a better syntax.

I'm not particularly attached to either syntax, as long as it's clearly documented and explicitly visible in the code (as opposed to something that kinda-sorta-but-doesn't-really-look-like-a-marker type syntax).

So, we'd have to give up some "natural-ness", as *that's the point* of having a syntax. And i'm certainly OK with comment style like this:

/*
* some meaningful comment.
* :doc_marker:
*/
...
/* :end doc_marker: */

Quoted 2 lines

Or something to that effect. What i'm *not* in favor of is these markers looking like part of a comment, rather than markers for doxygen.

I agree I think the scissors syntax would be a good mix between having a marker and keeping it as a natural comment while also showing that it isn’t a standard comment.

Thanks for everyone's feedback and input.
I will send a v2 based on these comments.

Thanks,
Conor.

#16 lore raw

PATCH v2 1/1 doc/contributing/doc: add info about including code

In reply to Conor Walsh

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.
Note: literalinclude was used in the past and was removed from DPDK as it created an issue with PDF generation but this is not done anymore:
git.dpdk.org/dpdk/commit/?id=d3ce1dc637c1bbef9a407f10281b2bc0549256da

---

v2: indentation changes and moved to scissor syntax

---
 doc/guides/contributing/documentation.rst | 56 +++++++++++++++++++++++
 1 file changed, 56 insertions(+)
doc/guides/contributing/documentation.rst +56 -0
index a4e6be6aca..7a0df1dc47 100644
@@ -433,6 +433,62 @@ Code and Literal block sections
          return 0;
       }
 
+* Code snippets can also be included directly from the code using the ``literalinclude`` block.
+  Using this block instead of a code block will ensure that the code snippets shown in the
+  documentation are always up to date with the code.
+
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+         :language: c
+         :start-after: Display the port MAC address.
+         :end-before: Enable RX in promiscuous mode for the Ethernet device.
+         :dedent: 1
+
+  This would be rendered as:
+
+  .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+     :language: c
+     :start-after: Display the port MAC address.
+     :end-before: Enable RX in promiscuous mode for the Ethernet device.
+     :dedent: 1
+
+  Specifying ``:language:`` will enable syntax highlighting for the specified language.
+  ``:dedent:`` is used in this example to remove 1 leading tab from each line of the snippet.
+
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include
+  explicit tags in your code to denote these locations for documentation purposes.
+  The accepted format for these comments is:
+
+     * Before the code snippet create a new comment, which is a sentence explaining
+       what the code snippet contains. The comment is terminated with a scissors ``8<``.
+     * After the code snippet create another new comment, which starts with a
+       scissors ``>8``, then ``End of`` and the first comment repeated.
+     * The scissors should be orientated as shown to make it clear what code is being snipped.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* Example feature being documented. 8< */
+    foo(bar);
+    /* >8 End of example feature being documented. */
+
+  ``foo(bar);`` could then be included in the docs using::
+
+      .. literalinclude:: ../../../examples/sample_app/main.c
+         :language: c
+         :start-after: Example feature being documented. 8<
+         :end-before: >8 End of example feature being documented.
+
+  If a multiline comment is needed before the snippet then the last line of the
+  multiline comment should be in the same format as the first comment shown
+  in the example.
+
+* More information about the ``literalinclude`` block can be found within the
+  `Sphinx Documentation <https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html?highlight=literalinclude#directive-literalinclude>`_.
 
 * The default encoding for a literal block using the simplified ``::``
   directive is ``none``.
Signature · 1 line
2.25.1
Acked-by on v2 1/1
#17 lore raw

Re: [dpdk-dev] [PATCH v2] doc/contributing/doc: add info about including code

Quoted 47 lines · Conor Walsh

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.
Note: literalinclude was used in the past and was removed from DPDK as it created an issue with PDF generation but this is not done anymore:
git.dpdk.org/dpdk/commit/?id=d3ce1dc637c1bbef9a407f10281b2bc0549256da

---

v2: indentation changes and moved to scissor syntax

---
  doc/guides/contributing/documentation.rst | 56 +++++++++++++++++++++++
  1 file changed, 56 insertions(+)
doc/guides/contributing/documentation.rst +16 -0
index a4e6be6aca..7a0df1dc47 100644
@@ -433,6 +433,62 @@ Code and Literal block sections
           return 0;
        }
 
+* Code snippets can also be included directly from the code using the ``literalinclude`` block.
+  Using this block instead of a code block will ensure that the code snippets shown in the
+  documentation are always up to date with the code.
+
+  The following will include a snippet from the skeleton sample app::
+
+      .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+         :language: c
+         :start-after: Display the port MAC address.
+         :end-before: Enable RX in promiscuous mode for the Ethernet device.
+         :dedent: 1
+
+  This would be rendered as:
+
+  .. literalinclude:: ../../../examples/skeleton/basicfwd.c
+     :language: c

doc/guides/contributing/documentation.rst Code and Literal block sections · line 461

Quoted 10 lines
doc/guides/contributing/documentation.rst +10 -0
+     :start-after: Display the port MAC address.
+     :end-before: Enable RX in promiscuous mode for the Ethernet device.
+     :dedent: 1
+
+  Specifying ``:language:`` will enable syntax highlighting for the specified language.
+  ``:dedent:`` is used in this example to remove 1 leading tab from each line of the snippet.
+
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include

Such lenient attitude still leaves room for accidental changes (e.g. typo fixes, whitespace reformatting, language fixes etc.). I would've preferred the scissor syntax to be mandatory for all comment snippets for documentation. However, risk is pretty low so i can live with that :)

Quoted 24 lines
doc/guides/contributing/documentation.rst +23 -0
+  explicit tags in your code to denote these locations for documentation purposes.
+  The accepted format for these comments is:
+
+     * Before the code snippet create a new comment, which is a sentence explaining
+       what the code snippet contains. The comment is terminated with a scissors ``8<``.
+     * After the code snippet create another new comment, which starts with a
+       scissors ``>8``, then ``End of`` and the first comment repeated.
+     * The scissors should be orientated as shown to make it clear what code is being snipped.
+
+  This can be done as follows:
+
+  .. code-block:: c
+
+    /* Example feature being documented. 8< */
+    foo(bar);
+    /* >8 End of example feature being documented. */
+
+  ``foo(bar);`` could then be included in the docs using::
+
+      .. literalinclude:: ../../../examples/sample_app/main.c
+         :language: c
+         :start-after: Example feature being documented. 8<
+         :end-before: >8 End of example feature being documented.

doc/guides/contributing/documentation.rst Code and Literal block sections · line 494

Quoted 10 lines
doc/guides/contributing/documentation.rst +7 -0
+
+  If a multiline comment is needed before the snippet then the last line of the
+  multiline comment should be in the same format as the first comment shown
+  in the example.
+
+* More information about the ``literalinclude`` block can be found within the
+  `Sphinx Documentation <https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html?highlight=literalinclude#directive-literalinclude>`_.
 
  * The default encoding for a literal block using the simplified ``::``
    directive is ``none``.
Signature · 2 lines
Thanks,
Anatoly
#18 lore raw

Re: [dpdk-dev] [PATCH v2] doc/contributing/doc: add info about including code

07/05/2021 11:54, Burakov, Anatoly:

Quoted 9 lines · Conor Walsh

doc/guides/contributing/documentation.rst Code and Literal block sections · line 461

Quoted 3 lines
doc/guides/contributing/documentation.rst +3 -0
+* ``start-after`` and ``end-before`` can use any text within a given file,
+  however it may be difficult to find unique text within your code to mark the
+  start and end of your snippets. In these cases, it is recommended to include

Such lenient attitude still leaves room for accidental changes (e.g. typo fixes, whitespace reformatting, language fixes etc.). I would've preferred the scissor syntax to be mandatory for all comment snippets for documentation. However, risk is pretty low so i can live with that :)

Compiling the documentation is part of any reasonnable test, so the risk is very low.

Quoted 2 lines
Quoted 2 lines
doc/guides/contributing/documentation.rst +2 -0
+  explicit tags in your code to denote these locations for documentation purposes.
+  The accepted format for these comments is:
Acked-by on v2 1/1 Applied
#19 lore raw

Re: [dpdk-dev] [PATCH v2] doc/contributing/doc: add info about including code

In reply to Conor Walsh

06/05/2021 18:40, Conor Walsh:

Quoted 12 lines

Currently the documentation describes how to add code snippets to the docs using code blocks. This can be problematic as the code snippets in the docs may fall out of sync with the actual code it is referencing within DPDK. This patch adds instructions to the contribution guide about how to include code in the docs using literalinclude which will dynamically get the code from source when the docs are generated. This will help to ensure that the code within the docs is up to date and not out of sync with the actual code.
Note: literalinclude was used in the past and was removed from DPDK as it created an issue with PDF generation but this is not done anymore:
git.dpdk.org/dpdk/commit/?id=d3ce1dc637c1bbef9a407f10281b2bc0549256da

Quoted 3 lines

Applied, thanks.