Controlling column width in restructuredText tables - restructuredtext

I have a table that looks like
==================== =======
Name1 Info1
Name2 Info2
Slightly longer name Info3
Name4 Info4
==================== =======
and rst2html insists on creating the HTML table with the following width specification:
<colgroup>
<col width="39%" />
<col width="61%" />
</colgroup>
This then makes the Slightly longer name in column 1 wrap into two lines.
How can I stop rst2html from doing this?

Not a great solution, but what about just filtering those lines out:
rst2html.py file.rst | egrep -v '(colgroup|col width)' > file.html

I have created a feature request for this at https://sourceforge.net/p/docutils/feature-requests/39/

Related

Memo-style header in RMarkdown to Word

I would like to include a memo style header in my Word document created with RMarkdown. The header would look like this:
<logo here> <Date here>
TO: John Doe, Frank Rich
FROM: Sally Slippers
CC: Lots of other, people, who, get, copied
SUBJECT: A line or two describing the memo
______________________________________________________________________
What is the best way to do this? Is there a way to use the YAML header for this? Or should I just create an inline "table" and completely bypass the header?

Multiple lines of text in single cell of simple table?

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.

Option lists in reStructuredText for command line arguments not prefixed with dashes or slashes

reStructuredText has nice support for option lists. For example, rst2html.py translates this RST markup
Options:
--foo does a foo
-b, --bar ABAR bar something
into the following nicely formatted HTML table:
<dt>Options:</dt>
<dd><table class="first last docutils option-list" frame="void" rules="none">
<col class="option" />
<col class="description" />
<tbody valign="top">
<tr><td class="option-group">
<kbd><span class="option">--foo</span></kbd></td>
<td>does a foo</td></tr>
<tr><td class="option-group">
<kbd><span class="option">-b</span>, <span class="option">--bar <var>ABAR</var></span></kbd></td>
<td>bar something</td></tr>
</tbody>
</table>
</dd>
This doesn't seem to extend naturally to positional arguments, however; for example
Arguments:
foo does a foo
bar ABAR bar something
renders as HTML completely lacking a table structure:
<dt>Arguments:</dt>
<dd>foo does a foo
bar ABAR bar something</dd>
Is there some way to produce an options list table for command line arguments that are not prefixed by dashes or slashes?
Yup. The rather limited syntax of option lists is not very well documented here:
http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html#option-lists
Here's the really irritating thing. Say you are writing up a series of options and some of them fit the syntax of an "option" per the preceding link, but some do not. For example --opt==keyword does (and "keyword" will be italicized whether you want it or not), but --pot=BLACK|KETTLE doesn't. Docutils will put all the ones that fit their syntax into a nice option-list <table> template, but where they don't, it drops out of the table format and codes them as standard <dl>s. So right in the middle of your stack of options are a couple that don't look like the others.

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)

Render a newline in my data inside a jqGrid cell

First off, I'm using version 3.7.1 with a jQuery UI framework theme. I'm trying to figure out how to have a newline or even a <br /> render inside of a jqGrid cell.
An example of what I'm looking to have happen:
________________________________________________________
Item 1 | some data | Applies to OS 1
Applies to OS 2
Applies to OS 3
Applies to OS 4
__________________________________________________________
Item 2 | some data | Applies to OS 1
__________________________________________________________
Item 3 | some data | Applies to OS 4
Applies to OS 5
__________________________________________________________
What currently happens when my data has either a <br /> or a \n is:
__________________________________________________________
Item 1 | some data | Applies to OS 1Applies to OS 2Applies to OS 3Applies to OS 4
I would rather not have to use an actual <br /> tag, since I'd rather not have HTML embedded in my data, but am willing to do whatever I have to since I NEED to render this data as a list of values. Thanks for any help.
You should use custom formatter (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:custom_formatter) for the column where you need to have <br />. This allow you to defina any HTML fragment for a column. See jqGrid: Editable column that always shows a select as an example.
Probably Wrapping Text lines in JqGrid will be also helpful for you.
P.S. If you will have problems with the usage of custom formatter, post a column definition and a raw data example (JSON data for example) for the column where you want have <br/>

Resources