2

I am trying to extract a specific code snippet (a test) from my sources to output it in the documentation.

TEST_CASE("std::vector") {
//! [MyTest]
  std::vector<int> v = {42,43};
  CHECK( v.size() == 2 );
//! [MyTest]
}

In doxygen, I can refer to it via a \snippet command.

Now, I would like to display the code inside the snippet directly into the sphinx documentation

You can create a std::vector just like this:

INSERT THE CODE BETWEEN [MyTest] MARKERS HERE

The std::vector class automatically allocates memory for you

How do I do that? I did not find any doxygensnippet directive in Breathe.

Note: I don't mind sphinx extracting the code for me directly with any kind of tag, I don't need to be doxygen-compliant.

Bérenger
  • 2,678
  • 2
  • 21
  • 42
  • 1
    See [`literalinclude` directive](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude). – Steve Piercy May 18 '20 at 11:17

1 Answers1

1

Thanks to @Steve_Piercy I was able to find what I wanted: use the literalinclude directive with start-after and end-before.

TEST_CASE("std::vector") {
//! [MyTest begin]
  std::vector<int> v = {42,43};
  CHECK( v.size() == 2 );
//! [MyTest end]
}

Documentation file:

You can create a std::vector just like this:

.. literalinclude::  my_test_file.cpp
  :start-after: MyTest begin
  :end-before: MyTest end

The std::vector class automatically allocates memory for you
Bérenger
  • 2,678
  • 2
  • 21
  • 42