how do I prepend a string to stdin in the command line? - command-line

This is probably pretty basic, but I'm struggling with the command line. Suppose I want to turn a markdown file myDoc.md to a pdf file. Markedjs provides a command line tool to convert markdown to html, and wkhtmltopdf can convert html to pdf, so I have the command
marked myDoc.md | wkhtmltopdf - myDoc.pdf
That works, it generates the pdf. But the pdf is pretty ugly, I want to prepend a style section to the html before passing it to wkhtmltopdf. Yes I could put the style section in the markdown document, but I don't want to pollute the markup with this. I want to use marked to generate html, then prepend a style section, then feed that to wkhtmltopdf, without any intermediate files to clean up. Something like this pseudo code
myStyle="<style>
*{
font-family: arial;
}
h1{
text-align:center;
}
</style>"
marked myDoc.md | concatenatestrings myStyle - | wkhtmltopdf - myDoc.pdf
but where I'm having trouble is I don't know how to handle the multiline string for myStyle and finding something that does what the hypothetical concatenatestrings command does, taking a string from stdin, prepend myStyle, and output to stdout.

I would use a template file, then use a subshell to output the template and output of marked myDoc.md to stdout, then pipe the results to the rest of your chain.
So, let us create the template file...
template.html
<style>
*{
font-family: arial;
}
h1{
text-align:center;
}
</style>
...and use it
$ (cat template.html && marked myDoc.md) | wkhtmltopdf - myDoc.pdf
I haven't tested this with your command (I don't want to install marked just to test it), but have tested it with the following...
$ (echo ree && echo cola) | cat
ree
cola

Related

How to generate caption from img alt atribute

Is there a way to convert an img tag containing an alt attribute (in a html file),
<img src="pics/01.png" alt="my very first pic"/>
to an image link plus caption (org file),
#+CAPTION: my very first pic
[[pics/01.png]]
using pandoc?
I'm calling pandoc like this:
$ pandoc -s -r html index.html -o index.org
where index.html contains the img tag from above, but it doesn't add the caption in the output org file:
[[pics/01.png]]
Currently the Org Writer unfortunately throws away the image alt and title strings. Feel free to submit an issue or patch if there's a way to do alt text in Org.
You can also always write a filter to modify the doc AST and add the alt text to an additional paragraph.
OP here. I didn't manage to make pandoc bend to my needs in this case. But a little bash scripting with some awk help does the trick.
The script replaces all img tags with org-mode equivalents plus captions. Pandoc leaves these alone when converting from html to org-mode.
The awk script,
# replace_img.awk
#
# Sample input:
# <img src="/pics/01.png" alt="my very first pic"/>
# Sample output:
# #+CAPTION: my very first pic
# [[/pics/01.png]]
BEGIN {
# Split the input at "
FS = "\""
}
# Replace all img tags with an org-mode equivalent.
/^<img src/{
print "#+CAPTION: " $4
print "[["$2"]]"
}
# Leave the rest of the file intact.
!/^<img src/
and the bash script,
# replace_img.sh
php_files=`find -name "*.php"`
for file in $php_files; do
awk -f replace_img.awk $file > tmp && mv tmp $file
done
Place these files at the root of the project, chomod +x replace_img.sh and then run the script: ./replace_img.sh. Change the extension of the files, if needed. I've had over 300 php files.

How can I save Perl/Expect output that contains mixed ascii content?

I have a perl script that uses the expect library to login to a remote system. I'm getting the final output of the interaction with the before method:
$exp->before();
I'm saving this to a text file. When I use cat on the file it outputs fine in the terminal, but when I open the text file in an editor or try to process it the formatting is bizarre:
[H[2J[1;19HCIRCULATION ACTIVITY by TERMINAL (Nov 6,14)[11;1H
Is there a better way to save the output?
When I run enca it's identified as:
7bit ASCII characters
Surrounded by/intermixed with non-text data
you can remove none ascii chars.
$str1 =~ s/[^[:ascii:]]//g;
print "$str1\n";
I was able to remove the ANSI escape codes from my output by using the Text::ANSI::Util library's ta_strip() function:
my $ansi_string = $exp->before();
my $clean_string = ta_strip($ansi_string);

doxygen: how to output backticks in a code section

i was wondering if it is possible to output backticks whithin a doxygen' code section.
~~~~~~~~~~
for file in `ls dir/*.filter`
do
done
~~~~~~~~~~
I get no output at all. And this seems to be caused by the backtick "`" i've inserted into my code section.
Does anyone had the same issue. Any suggestion?
many thanks
` is used to create an inline code block. Instead, use \code, \endcode rather than a markdown code block.
for example
\code
this is an inline `code block with ` characters
\endcode
renders with the ` characters included.
When a pair of `s is encountered in the code, doxygen will not process whatever is between.
The following will render correctly:
\code
for file in `ls dir/*.filter`
do
done
\endcode

php gettext include string with phpcode

i'm trying to use gettext to translate the string in my site
gettext doesn't have problem detecting strings such as
<? echo _("Donations"); ?>
or
<? echo _("Donate to this site");?>
but obviously, usually we'll use codes like this in our site
<? echo _("$siteName was developed with one thing in mind"); ?>
Of course in the website, the $siteName is displayed correctly as
My Website was developed with one thing in mind
if we put
$siteName = "My Website";
previously.
My problem is, i'm using poedit to extract all the strings in my codes that needs to be translated, and it seems poedit doesn't extract all string with php codes like I described above. So how do I get poedit extract strings with php code inside it too? Or is there any other tools I should use?
One possibility is to use sprintf. Just make sure you keep the percent (%) in the poedit string!
echo sprintf( _("This %s can be translated "), 'string');
Or when using multiple variables
echo vsprintf( _("This %s can be %s"), ['string', 'translated']);

Can I write doxygen alias similar to #code or #verbatim?

I need an alias to mark command-line code, set on a black background with white text, to be used like this:
#cmd
C:\temp>echo Hello, world!
Hello, world!
C:\temp>
#endcmd
Ordinary doxygen's aliases can't do this (multiline, nested "\temp"), but #code and #verbatim can. However, I can't use them because they are formatted as a white background with black text, so overwriting pre.fragment class with a custom CSS is not correct.
Any ideas?
UPD:
Comments showed how bad my English is...
Well, try again. Regular doxygen's features like HTML and XML works as shown bellow:
cpp file doxygen produced index.html
/**
#mainpage main |
<pre> | <pre>C:>echo Hello, world!
C:\temp>echo Hello, world! | Hello, world!</pre>
Hello, world! |
|
C:\temp> | <pre>C:></pre>
</pre> |
*/ |
in the log:
/tmp/index.h:3: warning: Found unknown command `\temp'
/tmp/index.h:6: warning: Found unknown command `\temp'
"code" and "verbatim" works different! Feel the difference:
cpp file doxygen produced index.html
/**
#mainpage main |
#verbatim | <div class="fragment">
C:\temp>echo Hello, world! | <pre class="fragment">C:\temp>echo Hello, world!
Hello, world! | Hello, world!
|
C:\temp> | C:\temp>
#endverbatim | </pre>
*/ | <div>
The question is: Can I write alias, that will work like "code" or "verbatim". Is it clean now?
What about just using and HTML div with your own CSS markup added to the Doxygen css file. It seems about the same amount of typing.
This answer summarises some the the comments in Paul Joireman's answer.
Paul Joireman's answer is the way to go. Define two aliases in your doxygen configuration file:
ALIASES += "mycode=<div class="myfragment"><pre class=myfragment>"
ALIASES += "endmycode=</pre></div>"
and the wrap your code example in \mycode and \endmycode statements. For example, your C++ file could look something like
/** \mainpage main
* \mycode
* C:\\temp>echo Hello, World!
* Hello, World!
*
* \endmycode
*/
which yields the following corresponding doxygen HTML output:
<div class="myfragment>"><pre class="myfragment>">
C:\temp>echo Hello, World!
Hello, World!</pre></div>
(I'm not sure why there are >'s in the class="myfragments>" parts). You will need to format the myfragments class in the CSS file.
The one additional thing in the above C++ code that wasn't mentioned in the comments is the use of the escaped backslash \\ in the code example.
The only wayout i found is
ALIASES += cmd="<div class=\"cmd\">#verbatim"
ALIASES += endcmd="#endverbatim</div>"
Where, CSS "cmd" class is set in your own stylesheet. May be:
.cmd { color:#e3e3e3; background-color:#222222; }