Re: include :code: eats tabs

Guenter Milde via Docutils-users <[email protected]>
Newsgroups gmane.text.docutils.user
Message-ID <[email protected]>
Dear Chris,

On 2018-08-28, Chris Pickel wrote:

> [-- Type: text/plain, Encoding: quoted-printable --]

> I tried using the “include” directive with :code: and :tab-width: -1.
> For some reason, all of the tabs disappeared.
...
> If I remove :tab-width: -1, Pygments no longer detects the tabbed
> lines as errors, but the quoted output still can’t be copy-pasted, as
> hard tabs are required in Makefiles.

Thank you for reporting the problem. Indeed, negative values for the
"tab-width" option work only with files included as "literal".
This restriction is currently not documented in 
http://docutils.sourceforge.net/docs/ref/rst/directives.html#including-an-external-document-fragment

Literal tabs can be preserved in "code" inclusions, too ---
please try the following patch for the directive code:

diff --git a/trunk/docutils/docutils/parsers/rst/directives/misc.py b/trunk/docutils/docutils/parsers/rst/directives/misc.py
index 638974230..fab450a6d 100644
--- a/trunk/docutils/docutils/parsers/rst/directives/misc.py
+++ b/trunk/docutils/docutils/parsers/rst/directives/misc.py
@@ -144,6 +144,9 @@ class Include(Directive):
             return [literal_block]
         if 'code' in self.options:
             self.options['source'] = path
+            # Convert tabs to spaces, if `tab_width` is negative.
+            if tab_width < 0:
+                include_lines = rawtext.splitlines()
             codeblock = CodeBlock(self.name,
                                   [self.options.pop('code')], # arguments
                                   self.options,


LaTeX ignores leading whitespace, so the following is a workaround to
get a visible representation of literal tabs in LaTeX-generated PDF (there
is no easy way to achieve preservation of TAB characters in drag and drop
from the PDF, though).

diff --git a/trunk/docutils/docutils/writers/latex2e/__init__.py b/trunk/docutils/docutils/writers/latex2e/__init__.py
index 0463e3504..fafab2c34 100644
--- a/trunk/docutils/docutils/writers/latex2e/__init__.py
+++ b/trunk/docutils/docutils/writers/latex2e/__init__.py
@@ -1516,6 +1516,10 @@ class LaTeXTranslator(nodes.NodeVisitor):
                 table[ord('>')] = ur'\textgreater{}'
         if self.insert_non_breaking_blanks:
             table[ord(' ')] = ur'~'
+            # tab chars may occur in included files (literal or code)
+            # quick-and-dirty replacement with spaces
+            # (for better results use `--literal-block-env=lstlisting`)
+            table[ord('\t')] = u'~' * self.settings.tab_width
         # Unicode replacements for 8-bit tex engines (not required with XeTeX/LuaTeX):
         if not self.is_xetex:
             if not self.latex_encoding.startswith('utf8'):

sincerely,

Günter


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Docutils-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.