Extracting SQL code fron rst files

49 views
Skip to first unread message

Paolo Cavallini

unread,
Jul 7, 2014, 4:21:44 PM7/7/14
to sphinx-dev
Hi all.
I have a few rst files with some SQL scripting inside.
How can I extract only SQL code from them?
Thanks a lot.

--
Paolo Cavallini http://www.faunalia.it/pc

gilberto dos santos alves

unread,
Jul 7, 2014, 5:42:48 PM7/7/14
to sphin...@googlegroups.com
please could you post someone for example? thanks.


--
You received this message because you are subscribed to the Google Groups "sphinx-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
gilberto dos santos alves
+55.11.98646-5049
sao paulo - sp - brasil




Markus Zapke-Gründemann

unread,
Jul 8, 2014, 4:57:39 AM7/8/14
to sphin...@googlegroups.com
Paolo Cavallini schrieb:
> I have a few rst files with some SQL scripting inside.
> How can I extract only SQL code from them?
> Thanks a lot.
Instead of extracting the SQL queries from your documentation you save all SQL
queries in a file and include it using the literalinclude directive[1]. You can
use the :lines: option to show only selected lines from the file.


Regards

Markus

[1] http://sphinx-doc.org/markup/code.html#directive-literalinclude

Paolo Cavallini

unread,
Jul 15, 2014, 10:32:54 AM7/15/14
to sphinx-dev
Hi Markus,
thanks for the suggestion. I'm studying the solution, but I think it does not fit well in my workflow: I have many short queries interspersed in the text, and the maintenance (and readability) of source will be quite cumbersome. Referring to line numbers is unfeasible (addin a line at the beginning will cause all other lines to shift, and will require to fix all literalinclude. Prepend and append would also require to add lots of references in the sql file.
In principle, Sphinx recognizes all my SQL, so it should be possible to extract it on a separate file at compile time.
Is this a missing feature, or there are other solutions available?
Thanks in advance for any hint.
All the best.


--
You received this message because you are subscribed to the Google Groups "sphinx-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Lehmann

unread,
Jul 15, 2014, 3:35:16 PM7/15/14
to sphin...@googlegroups.com
If you have a lot of code snippets you could use individual files, say from a dedicated folder.  That sounds like the sanest solution.

For keeping everything in a single file, you could introduce markers and use :start-after:., like:

# doc.rst
SELECT works like this:

.. literal-include:: all.sql
   :start-after: select-statement-simple
   :end-before: ---

# all.sql
select-statement-simple
SELECT * FROM table
---
select-statement-advanced
---

I agree that this is non-optimal.

The doctree has all information you want, though, and if you spare the time to write a custom plugin it should be easy to achieve.  Let's take a look at this example:

.. code:: sql

   SELECT * FROM `table`

Is parsed as the following Docutils tree:

<literal_block classes="code sql" xml:space="preserve">
     <inline classes="keyword">
         SELECT
     <inline classes="operator">
         *
     <inline classes="keyword">
         FROM
     <inline classes="keyword">
         table

So, definitely possible to write a builder that queries (doctree.traverse) all literal_blocks that have the sql class.  I think it's hardly a missing feature in Sphinx, but just out of scope.

Cheers,
Robert

Paolo Cavallini

unread,
Jul 16, 2014, 2:53:27 AM7/16/14
to sphinx-dev
OK, thanks a lot for your comments.
Reply all
Reply to author
Forward
0 new messages