Is it possible to preserve long lines in Sphinx? - python-sphinx

My restructuredText input file to Sphinx has a long line:
This is a long line text that I want to keep as a long single line. It should not be wrapped in the text output.
Using the text builder of Sphinx, I get this output:
This is a long line text that I want to keep as a long single line. It
should not be wrapped in the text output.
Sphinx wraps the line.
I would like to keep the single long line as is. Is this possible?
Using Line Blocks was suggested in this answer. However using line blocks changes the indentation of the line. I could not find a way to keep the indentation same with Line Blocks.
For input:
Prev Line
| This is a long line text that I want to keep as a long single line. It should not be wrapped in the text output.
Next Line
I get this output:
Prev Line
This is a long line text that I want to keep as a long single line. It should not be wrapped in the text output.
Next Line
The long line is kept long but it is indented now.
I was not able to find a setting about line wrapping in Sphinx configuration page.
I am using sphinx-build version 1.7.4 and the command I use is:
sphinx-build -M text "." "_build"
Update:
The code-block directive also indents the long line.
Input:
Prev Line
.. code-block:: text
This is a long line text that I want to keep as a long single line. It should not be wrapped in the text output.
Next Line
Output:
Prev Line
This is a long line text that I want to keep as a long single line. It should not be wrapped in the text output.
Next Line

Monkey-patching the rendering of line blocks for Text writer works for me:
add this to conf.py
def my_visit_line_block(self, node):
# type: (nodes.Node) -> None
self.new_state(indent=0)
self.lineblocklevel += 1
def setup(app):
from sphinx.writers.text import TextTranslator
TextTranslator.visit_line_block = my_visit_line_block
The obvious defect is that it modifies rendering of all line blocks...
I am not familiar enough about sphinx.writers.text (and docutils) to see immediately if there is better way than the above monkey-patching.
I could re-insert indentation via this mark-up:
| \ one
| \ two
| \ three
but being forced to modify mark-up is bad.

Related

How to detect a blank line between filled lines in .txt file and convert it to a single tab

I an running a bash/.dat script (Mac terminal) and part of it is converting each line return into a TAB (to get it ready for nicely importing into Excel). The problem is that I also want to remove all extra blank lines except a single blank line when comes between two filled lines. So...
Line pre-A is blank
Line A has text
Line B has text
Line C is blank
Line D has text
Line E is blank
Line F is blank
Line C above would become a TAB and Line E and F (and pre-A) would be deleted. Also, sometimes there is a blank line before Line A (labelled Line pre-A above), so I'd want it removed but not replaced with a TAB.
So the result would be:
Line A text [TAB] Line B text [TAB] [TAB] Line D text
...and it'd be OK if Line D text was followed by a [TAB]. Make sense? Is this doable and, if so, how?
Thanks!
If perl is your option, would you please try:
perl -0777pe 's/^\n+//; s/\n{3,}/\t/g; s/\n/\t/g' file.txt
The -0777 option tells perl to slurp all lines at once to process
newline characters between lines.
The -pe option enables the one-liner programming.
The first substitution s/^\n+// removes the pre blank line(s).
The next s/\n{3,}/\t/g converts three or more consecutive newline
characters (meaning two or more blank lines) into a tab character.
The last s/\n/\t/g converts the newline characters into the same number
of tab characters.

SED Ensure XML insert not inside comment

I am working with stock RHEL7/8 tools, and writing a script that will add a piece to a config file that is formatted as XML. I have run into a case where my sed statement can insert the added text inside a comment.
My current sed command gets the last existence of the tag <Program> and inserts the new tag after its closing tag </Program>.
How can I account for this possibly, but not always being inside a comment?
My script:
sed -i '0,/<Program id/s// <Program id=\"myProgram\"> <\/Program>' filepath
XML Example (displays the error inserting inside comment):
<Program id="myProgram"></Program>
<!--
<Program id="commentedOutProgram"></Program>
<Program id="newlyAddedProgram"><Program>
-->
EDIT:
This is happening at install time. I would like to add a way for some RHEL 7/8 built in tool to look in the XML file, make sure it's not in a comment, and add the new contents
Have a go with this. The usual caveats apply: It probably only works for exactly the sample you provided. Use a proper XML tool if you need a robust solution.
sed -e '/<!--/,/-->/b' \
-e '0,\%<Program id="[^"]*"></Program>%s%<Program id="myProgram"> </Program>%' filepath
Your original script seemed to have several errors, so I couldn't copy it verbatim, but this should at least give you an idea of how to modify it: add a b to skip any lines between <!-- and -->.
The % separators are just to avoid having to backslash slashes; sed allows you to use any separator you like instead of a slash, you just have to backslash the first one.
The b command jumps to a label; if the label is not specified, it jumps to the end of the script, i.e. skips the substitution part and starts over with the next line. The address expression before b selects any comment region, i.e. any lines between a line matching <!-- and a line matching -->.

How do I delete all lines from a file after (and including) a line that contains a defined string in a Bash script?

I'm hacking about a text file in the middle of a Bash script (on an RPI3B+ with OSMC installed) and trying to crop a file at the first line that contains the text "BLAH DE BLAH" (deleting everything in the same file after and including the first line it finds that text on).
For example (in the file filename.text):
This is the first line
This is the second line
This is the third line containing "BLAH DE BLAH"
This is the fourth line
This is the fifth line
Required output (in the file filename.text):
This is the first line
This is the second line
I've tried to investigate awk and sed related posts, but I'm finding it all so confusing as I can't find anything that does exactly what I need (some split at certain line numbers, some from the command line not a bash script, some before and after certain strings)... and I'm stuck. As you can see, I can't even work out how to format this post properly (my head hurts so much)!
Any help appreciated - thanks!
Looks like
sed '/BLAH DE BLAH/Q'
would do the job in GNU sed.

Adding new line/line break while writing swirl lesson "lesson.yaml" with Class "text" in RStudio

I am writing a swirl lesson using swirlify package functions in RStudio.
Below is how lesson.yaml file looks like now
- Class: text
Output: Welcome to Part 1 Playing with Numbers!!!
Output for which looks like
How to insert a new line or line break after Welcome to Part 1 in lesson.yaml file above, so that it displays the output as below when I run the demo_lesson() command again after saving the lesson.yaml file
| Welcome to Part 1
| Playing with Numbers!!!
Using YAML, you can use any of these equivalent approaches:
Quoted string with escape
- Class: text
Output: "Welcome to Part 1\nPlaying with Numbers!!!"
Literal scalar
- Class: text
Output: |-
Welcome to Part 1
Playing with Numbers!!!
(| starts a literal scalar and - tells YAML to drop the final line break.)
Multiline scalar
- Class: text
Output:
Welcome to Part 1
Playing with Numbers!!!
(since one line break gets folded into a space, you need two line breaks.)
Since I do not know whether swirlify nicely handles line breaks in the string, I guess you could also do
- Class: text
Output: Welcome to Part 1
- Class: text
Output: Playing with Numbers!!!
Thanks to flyx for answering the question, here is how it works!!
I. Quotes string with escape (Works with two \n\n)
lesson.yaml file
II. Literals
First line in Output: |- Hit ENter once
Indent once by pressing one Tab for first line, Hit Enter twice to have break between header line and paragraph like below, then it works..
lesson.yaml file
III. Mulitline Scalar (Works with three times Enter between two lines)
Press Enter once After Output: in lesson.yaml
Indent once by pressingTab` key once, Write your first line, hit Enter thrice and write the second line. Then it works.
lesson.yaml file
OUTPUT FOR ALL THE ABOVE ANSWERS

What changes when a file is saved in Kedit for windows that the unix2dos command doesn't do?

So I have a strange question. I have written a script that re-formats data files. I basically create new files with the right column order, spacing, and such. I then unix2dos these files (the program I am formatting these files for is DIPS for windows, and I assume that the files should be ansi). When I go to open the files in the DIPS Program however an error occurs and the file won't open.
When I create the same kind of data file through the DIPS program and open it in note pad, it matches exactly with the data files I have created with my script.
On the other hand if I open the data files that I have created with my script in Kedit first, save them, and then open them in the DIPS program everything works.
My question is what could saving in Kedit possibly do that unix2dos does not?
(Also if I try using note pad or word pad to save instead of Kedit the file doesn't open in DIPS)
Here is what was created using the diff command in unix
"
1,16c1,16
* This file is generated by Dips for Windows.
* The following 2 lines are the Title of this file.
Cobre Panama
Drill Hole B11106-GT
Number of Traverses: 0
Global Orientation is:
DIP/DIPDIRECTION
0.000000 (Declination)
NO QUANTITY
Number of extra columns are: 0
--
* This file is generated by Dips for Windows.
* The following 2 lines are the Title of this file.
Cobre Panama
Drill Hole B11106-GT
Number of Traverses: 0
Global Orientation is:
DIP/DIPDIRECTION
0.000000 (Declination)
NO QUANTITY
Number of extra columns are: 0
18c18
--
440c440
--
442c442
-1
-1
"
Any help would be appreciated! Thanks!
Okay! Figured it out.
Simply when you unix2dos your file you do not strip any space characters in between the last letter in a line and the line break character. When saving in Kedit you do strip the spaces between the last letter in a line and the line break character.
In my script I had a poor programing practice in which I was writing a string like this;
echo "This is an example string " >> outfile.txt
The character count is 32, and if you could see the break line character (chr(10)) the line would read;
This is an example string
If you unix2dos outfile.txt the line looks the same as above but with a different break line character. However when you place the file into Kedit and save it, now the character count is 25 and the line looks like this;
This is an example string
This occurs because Kedit does not preserve spaces at the end of a line. It places the return or line break character at the last letter or "non space" character in a line.
So programs that read literal input like DIPS (i'm guessing) or more widely used AutoCAD scripting will have a real problem with extra spaces before the return character. Basically in AutoCAD scripting a space in a line is treated as a return character. So if you have ten extra spaces at the end of a line it's treated the same as ten returns instead of the one you probably intended.
OH and if this helped you out or though it was good please give me a vote up!
unix2dos converts the line-break characters at the end of each line, from unix line breaks (10) to dos line breaks (13, 10)
Kedit could possible change the encoding of the file (like from ansi to UTF-8)
You can change the encoding of a file with the iconv utility (on a linux box)

Resources