Multiple lines of text in single cell of simple table? - restructuredtext

I found this question, but I don't want explicit <br>s in my cell; I just want it to line-wrap where necessary.
e.g.,
================ ============
a short sentence second cell
a much longer bottom right
sentence
================ ============
I want "a much longer sentence" to all fit in one cell. I'd need to use very long lines of text unless I can find a way to wrap it. Is this possible?
I'm using NoTex w/ PDF output if relevant.

There is a clean way. The issue is by default the columns are set to no-wrap, so that's why you get the scroll. To fix that you have to override the css with the following:
/* override table no-wrap */
.wy-table-responsive table td, .wy-table-responsive table th {
white-space: normal;
}

The simple table style does not support wrapping blocks. Use the grid style instead, like this:
+------------------+--------------+
| a short sentence | second cell |
+------------------+--------------+
| a much longer | bottom right |
| sentence | |
+------------------+--------------+
These tables are more tedious to work with, but they're more flexible. See the full documentation for details.

A workaround for this problem is to use a replace directive:
================ ============
a short sentence second cell
|long_sentence| bottom right
================ ============
.. |long_sentence| replace:: a much longer sentence

The example ddbeck presented may work because the sentence is to short. In the case of the lenght of the sentence dont fit in the screen, the sentence will not continue in a new line. Instead, the table will create a horizontal scrollbar. There is no clean way for solving this problem. You can implicit use pipe to implicitly change line like you saw here.
If you want alternatives to write your tables in restructuredtext, more pratical ways, you can check it in Sphinx/Rest Memo.

I wrote a python utility to format fixed-width plaintext table with multiline cells: https://github.com/kkew3/tabulate. Hope it helps.

Related

How to increase row height in vaadin grid accordingly to data in the cell

I am consuming data from the list and some list may contain multiple line data, so how that can be reflected in the vaadin grid at runtime.
This is the code
public Grid GridBasic() {
grid.removeAllColumns();
grid.addColumn(TopicConsumedDetails::getPartitionNumber).setHeader("PARTITION ");
grid.addColumn(TopicConsumedDetails::getOffset).setHeader("OFFSET");
grid.addColumn(TopicConsumedDetails::getMessage).setHeader("MESSAGE");
grid.setItems(details);
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
grid.getColumns().forEach( col -> col.setAutoWidth(true));
return grid;
}
This just displays all the data in a single line and it requires scrolling left to right.
Vaadin Version :23.3.1
Use the built-in "wrap cell content" variant: https://vaadin.com/docs/latest/components/grid/#wrap-cell-content
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
As per the previous answer, I think that using this is the correct approach:
grid.addThemeVariants(GridVariant.LUMO_WRAP_CELL_CONTENT);
However, you are overriding this setting by calling
grid.getColumns().forEach( col -> col.setAutoWidth(true));
According to the documentation, this automatically sets the column width based on the contents, leading to the right scroll problem.
If you remove this call, you should get the proper wrapping behavior. I was able to reproduce the problem and then see a result like this once I removed the auto width setting:
Alternatively, if you have sensible line breaks in the content you want to wrap, you can do that--but it won't happen automatically, as mentioned by #Rolf in a comment above. That is because the line breaks in the text are basically just whitespace and aren't respected as such by the HTML. So in order to do that, one option is to add an "Html" component column. You can then replace your text's line breaks with <br/> tags, which you could do with a regular expression. It would look like this:
String myColumnText = colText.replaceAll(....); //Some regex to match on your particular line breaks
grid.addComponentColumn(item -> new Html("<p>" + myColumnText +"</p>");
The <p> tags (or some wrapper tags) are required as the Html component requires a wrapper tag.
Note that (1) using this approach means that you won't get the automatic wrapping behavior any more so the line breaks in your source need to be sensible and (2) you have to be certain you trust the incoming content or it is otherwise sanitized, as this kind of rendering-text-as-html opens up some security holes with untrusted content.

How to preserve white space at the start of a line in .Rd documentation?

I need to indent some math stuff in the \details section of my .Rd documentation to enhance its readability. I am using mathjaxr. Is there any way to indent without installing roxygen2 or similar?
The math stuff is inline, so simply setting to display using \mjdeqn won't solve this.
I seem to have a reasonable "cheating" work around for indenting the first line using mathjaxr, at least for the PDF and HTML output.
We need to do two things:
Use the mathjax/LaTeX phantom command. phantom works by making a box of the size necessary to type-set whatever its argument is, but without actually type-setting anything in the box. For my purposes, if I want to indent, say, about 2 characters wide, I would start the line with a \mjeqn{\phantom{22}}{ } and following with my actual text, possibly including actual mathy bits. If I want an indent of, say, roughly 4 characters wide, I might use \mjeqn{\phantom{2222}}{ }.
Because mathjaxr has a problem with tacking on unsolicited new lines when starting a line with mjeqn, we need to prefix the use of phantom in 1 above with an empty bit of something non-mathjaxr-ish like \emph{}.
Putting it all together, I can indent by about 2 characters using something like this:
\emph{}\mjeqn{\phantom{22}}Here beginneth mine indented lineā€¦
I need to explore whether the { } business actually indents for ASCII output, or whether I might accomplish that using or some such.

Implementing syntax highlighting for markdown titles in PySide/PyQt

I am trying to implement a syntax highlighter for markdown for my project in PySide. The current code covers the basic, with bold, italic, code blocks, and some custom tags. Below is an extract of the relevant part of the current code.
What is blocking me right now is how to implement the highlighting for titles (underlined with ===, for the main title, or --- for sub-titles). The method that is used by Qt/PySide to highlight the text is highlightBlock, which processes only one line at a time.
class MySyntaxHighlighter(QtGui.QSyntaxHighlighter):
def highlightBlock(self, text):
# do something with this line of text
self.setCurrentBlockState(0)
startIndex = 0
if self.previousBlockState() != 1:
startIndex = self.blockStartExpression.indexIn(text)
while startIndex >= 0:
endIndex = self.blockEndExpression.indexIn(
text, startIndex)
...
There is a way to recover the previousBlockState, which is useful when a block has a defined start (for instance, the ~~~ syntax at the beginning of a code-block). Unfortunately, there is nothing that defines the start of a title, except for the underlining with === or --- that take place on the next line. All the examples I found only handle cases where there is a defined start of the expression, and so that the previousBlockState gives you an information (as in the example above).
The question is then: is there a way to recover the text of the next line, inside the highlightBlock? To perform a look-ahead, in some sense.
I though about recovering the document currently being worked on, and find the current block in the document, then find the next line and make the regular expression check on this. This would however break if there is a line in the document that has the exact same wording as the title. Plus, it would become quite slow to systematically do this for all lines in the document. Thanks in advance for any suggestion.
If self.currentBlock() gives you the block being highlighted, then:
self.currentBlock().next().text()
should give you the text of the following block.

Pipe a certain field of an input stream in a command, and paste back the results

Say I have an input stream consisting of lines separated into a certain number of fields. I would like to cut on the various fields, pipe a certain field (or fields) to a program (which is assumed to return one line for each input line) and leave the other fields as is, and paste the results back together. I can probably imagine convoluted solutions, but there ought to be a clean and natural way to do that.
As a specific example, say I have a program producing lines of the form:
$ inputprog
<a> hello world!
<b> hi everyone!
<a> hi!
Say I would like to put the message in uppercase while leaving the first field unchanged. Here is how I would imagine things:
$ inputprog | program -d' ' -f2- "tr a-z A-Z"
<a> HELLO WORLD!
<b> HI EVERYONE!
<a> HI!
I am looking for a reasonably clean way to approximate program. (I am not interested in solutions which are specific to this example.)
Thanks in advance for your help!
awk can do what you want. For example:
$ echo "field1 field2" | awk '{$2 = toupper($2); print;}'
field1 FIELD2
Comes pretty close to what you want to do. $2 = toupper($2); changes the second field, while print prints out the whole (modified) line.
However, you got a problem in how you define a 'field'. In the example above fields are separated by spaces (you can change the field separator to an arbitrary regexp with like so: -F'<[a-zA-Z]+>' - this would consider as a field separator).
But in your example you seem to view <a> as one field and hello world! as another one. Any program could only come to your desired behaviour by wild guessing that way. Why wouldn't world! be considered a third field?
So, if you can get input with a clear policy of separating fields, awk is exactly what you want.
Check out pages like http://people.cs.uu.nl/piet/docs/nawk/nawk_92.html (awk string functions) and http://www.pement.org/awk/awk1line.txt (awk 1 liners) for more information.
BTW, one could also make your specific example above work by looping over all the fields except the first one (NF == Number of Fields):
$ echo "<a> hello world!
<b> hi everyone!
<a> hi" |
awk '{for(i=2;i<=NF;++i) { $i=toupper($i); }; print;}'
<a> HELLO WORLD!
<b> HI EVERYONE!
<a> HI
Even though you are not interested in the solution to this example. ;-)
P.S.: sed should also be able to do the job (http://en.wikipedia.org/wiki/Sed)

Is there a way to right-justify text within cells of a restructured text table?

When I put numbers in tables I generally want them to be right justified and in a fixed width font. Otherwise the numbers become hard to compare.
Is there a way to right justify fields within a table?
Eg how do I get this to render with the numbers all right justified?
.. csv-table::
:header: x, x*x
3,9
4,16
9,81
10,100
The best solution I have found so far is to use sphinx: http://sphinx.pocoo.org/
This supports the tabularcolumns directive, this does the right thing with latex output.
.. tabularcolumns:: |r|r|
.. csv-table::
:header: x, x*x
3,9
4,16
9,81
10,100
I was able to solve a similar problem. In my case, I was using a grid table.
I noticed that I could provide HTML to be used when a restructuredText "line block" was used.
I put the following in my restructuredText source file:
.. raw:: html
<style> .line {text-align:right;} </style>
Then, in my table cells, I used the "|" character at the start (extreme left edge) of each cell that I wanted to be right-justified.
This hack does "take over" the formatting of the line block construct. That may or may not be acceptable in a given context.

Resources