Sascha
Sascha

Reputation: 1136

breakable slashes everywhere but URLs

I generate pdf (latex) from restructured text using python sphinx (1.4.6) .

I use narrow table column headers with texts like "stuff/misc/other". I need the slashes to be breakable, so the table headers don't overflow into the next column.

The LaTeX solution is to use \BreakableSlash or \slash where necessary. I can use python code to replace all slashes:

from sphinx.util.texescape import tex_replacements

# \BreakableSlash needs package hyphenat to be loaded
tex_replacements.append((u'/', ur'\BreakableSlash ') ) 
# tex_replacements.append((u'/', ur'\slash ') ) 

But that will break any URL like http://www.example.com/ into something like

http:\unhbox\voidb@x\penalty\@M\hskip\z@skip/\discretionary{-}{}{}\penalty\@M\hskip\z@skip\unhbox\voidb@x\penalty\@M\hskip\z@skip/\discretionary{-}{}{}\penalty\@M\hskip\[email protected]

or

http:/\penalty\exhyphenpenalty/\penalty\exhyphenpenaltywww.example.com

I'd like to use a general solution that works in both cases, where the editor of the documentation can still use normal ReST and doesn't have to worry about latex.

Any idea how to get classic slashes in URLs and breakable slashes everywhere else?

Upvotes: 1

Views: 199

Answers (2)

Sascha
Sascha

Reputation: 1136

One way to do it, might be to write a Transform subclass. And then use add transform in setup(app) to use it in every read.

I could use DefaultSubstitutions from transforms.py as template for my own class.

Upvotes: 0

tfv
tfv

Reputation: 6259

You have not really given data and source code and only asked for an idea, so I take the liberty of only sketching a solution in pseudo code:

  • Split the document into a list of strings at each position of a space using .split()
  • For each string, check whether it is an URL by comparing its left side to http:// (and maybe also ftp://, https:// or similar tags)
  • Do replacements, but only in strings which are no URLs
  • Recombine all strings including the spaces again, using a command such as " ".join(my_list)

Upvotes: 1

Related Questions