How to stop ImageMagick in Ruby (Rmagick) evaluating an # sign in text annotation - ruby

In an app I recently built for a client the following code resulted in the variable #nameText being evaluated, and then resulting in an error 'no text' (since the variable doesn't exist).
To get around this I used gsub, as per the example below. Is there a way to tell Magick not to evaluate the string at all?
require 'RMagick'
#image = Magick::Image.read( '/path/to/image.jpg' ).first
#nameText = '#SomeTwitterUser'
#text = Magick::Draw.new
#text.font_family = 'Futura'
#text.pointsize = 22
#text.font_weight = Magick::BoldWeight
# Causes error 'no text'...
# #text.annotate( #image, 0,0,200,54, #nameText )
#text.annotate( #image, 0,0,200,54, #nameText.gsub('#', '\#') )

This is the C code from RMagick that is returning the error:
// Translate & store in Draw structure
draw->info->text = InterpretImageProperties(NULL, image, StringValuePtr(text));
if (!draw->info->text)
{
rb_raise(rb_eArgError, "no text");
}
It is the call to InterpretImageProperties that is modifying the input text - but it is not Ruby, or a Ruby instance variable that it is trying to reference. The function is defined here in the Image Magick core library: http://www.imagemagick.org/api/MagickCore/property_8c_source.html#l02966
Look a bit further down, and you can see the code:
/* handle a '#' replace string from file */
if (*p == '#') {
p++;
if (*p != '-' && (IsPathAccessible(p) == MagickFalse) ) {
(void) ThrowMagickException(&image->exception,GetMagickModule(),
OptionError,"UnableToAccessPath","%s",p);
return((char *) NULL);
}
return(FileToString(p,~0,&image->exception));
}
In summary, this is a core library feature which will attempt to load text from file (named SomeTwitterUser in your case, I have confirmed this -try it!), and your work-around is probably the best you can do.
For efficiency, and minimal changes to input strings, you could rely on the selectivity of the library code and only modify the string if it starts with #:
#text.annotate( #image, 0,0,200,54, #name_string.gsub( /^#/, '\#') )

Related

Keep custom code block attributes in pandoc when converting to Markdown

I am converting an org file to Markdown (specifically commonmark). I am adding a custom attribute to my code blocks, which the commonmark writer does not support, and strips them from the code block during conversion. I am trying to find a way to keep my custom attributes.
This is what I have:
#+begin_src python :hl_lines "2"
def some_function():
print("foo bar")
return
#+end_src
This is what I want in my .md file:
``` python hl_lines="2"
def some_function():
print("foo bar")
return
```
After doing some research, I think a filter can solve my issue: I am now playing with panflute, a python lib for writing pandoc filters.
I found some relevant questions, but they apply to other conversions (rST -> html, rst -> latex) and I don't know enough Lua to translate the code into Python and the org -> md conversion.
Thanks for any help.
I was able to write a script, posting it here for future Python-based questions about pandoc filters.
The filter below requires panflute, but there are other libs for pandoc filters in Python.
import panflute
def keep_attributes_markdown(elem, doc, format="commonmark"):
"""Keep custom attributes specified in code block headers when exporting to Markdown"""
if type(elem) == panflute.CodeBlock:
language = "." + elem.classes[0]
attributes = ""
attributes = " ".join(
[key + "=" + value for key, value in elem.attributes.items()]
)
header = "``` { " + " ".join([language, attributes]).strip() + " }"
panflute.debug(header)
code = elem.text.strip()
footer = "```"
content = [
panflute.RawBlock(header, format=format),
panflute.RawBlock(code, format=format),
panflute.RawBlock(footer, format=format),
]
return content
def main(doc=None):
return panflute.run_filter(keep_attributes_markdown, doc=doc)
if __name__ == "__main__":
main()
You can now run the following command:
pandoc --from=org --to=commonmark --filter=/full/path/to/keep_attributes_markdown.py --output=target_file.md your_file.org

How to include the source line number everywhere in html output in Sphinx?

Let's say I'm writing a custom editor for my RestructuredText/Sphinx stuff, with "live" html output preview. Output is built using Sphinx.
The source files are pure RestructuredText. No code there.
One desirable feature would be that right-clicking on some part of the preview opens the editor at the correct line of the source file.
To achieve that, one way would be to put that line number in every tag of the html file, for example using classes (e.g., class = "... lineno-124"). Or use html comments.
Note that I don't want to add more content to my source files, just that the line number be included everywhere in the output.
An approximate line number would be enough.
Someone knows how to do this in Sphinx, my way or another?
I decided to add <a> tags with a specific class "lineno lineno-nnn" where nnn is the line number in the RestructuredText source.
The directive .. linenocomment:: nnn is inserted before each new block of unindented text in the source, before the actual parsing (using a 'source-read' event hook).
linenocomment is a custom directive that pushes the <a> tag at build time.
Half a solution is still a solution...
import docutils.nodes as dn
from docutils.parsers.rst import Directive
class linenocomment(dn.General,dn.Element):
pass
def visit_linenocomment_html(self,node):
self.body.append(self.starttag(node,'a',CLASS="lineno lineno-{}".format(node['lineno'])))
def depart_linenocomment_html(self,node):
self.body.append('</a>')
class LineNoComment(Directive):
required_arguments = 1
optional_arguments = 0
has_content = False
add_index = False
def run(self):
node = linenocomment()
node['lineno'] = self.arguments[0]
return [node]
def insert_line_comments(app, docname, source):
print(source)
new_source = []
last_line_empty = True
lineno = 0
for line in source[0].split('\n'):
if line.strip() == '':
last_line_empty = True
new_source.append(line)
elif line[0].isspace():
new_source.append(line)
last_line_empty = False
elif not last_line_empty:
new_source.append(line)
else:
last_line_empty = False
new_source.append('.. linenocomment:: {}'.format(lineno))
new_source.append('')
new_source.append(line)
lineno += 1
source[0] = '\n'.join(new_source)
print(source)
def setup(app):
app.add_node(linenocomment,html=(visit_linenocomment_html,depart_linenocomment_html))
app.add_directive('linenocomment', LineNoComment)
app.connect('source-read',insert_line_comments)
return {
'version': 0.1
}

tcpdf with laravel 5

i want to save the output pdf file to public folder my method is
public function qrSVG()
{
$qrCodes = ['4659284fff','465928447','465928447','613271980','484016586','aaaaabbbbbccccc'];
$id = ['201596400-1','201596400-2','201596400-3','831070646','493130428','aaaaabbbb'];
PDF::SetTitle('qrcodes\test');
$i=0;
foreach(array_chunk($qrCodes, 2) as $qrCodee)
{
PDF::AddPage();
$m = 55;
$n = 30;
foreach($qrCodee as $qr)
{
QrCode::size(400);
QrCode::margin(3);
QrCode::errorCorrection('H');
QrCode::encoding('UTF-8');
QrCode::backgroundColor(255,255,255);
QrCode::color(0,0,0);
QrCode::imageTitle($id[$i]);
$svg = QrCode::generate($qr);
PDF::ImageSVG('#'.$svg, $x=$m, $y=$n, $w='100', $h='100', $link='', $align='', $palign='', $border=1, $fitonpage=false);
$i++;
$n = 150;
}
}
ob_clean();
PDF::Output('qrcodes\test.pdf');}
this code generate and open the file put don't save it when i replace the last line in my code with PDF::Output('qrcodes\test.pdf', 'F');
when i put any option with PDF::Output there is an error with F and D options the error when use F is
ErrorException in tcpdf_static.php line 2440:
fopen(): remote host file access not supported, file://qrcodes\test.pdf
and when i replace the last line with
PDF::Output($_SERVER['DOCUMENT_ROOT'] . 'qrcodes\test.pdf', 'F');
the error is
ErrorException in tcpdf.php line 2793:
Undefined property: Elibyy\TCPDF\Pdf::$h
The main reason you're getting the error about remote host file access not supported is because you need to provide the full path in the file name that you provide to the Output() method. Yeah, it's a bit annoying and it catches me out all the time!
I can't comment on the second error you're getting because I cannot get your code to run (missing methods in QrCode class). What version are you using? Also, why are you trying to create an SVG for the QR code and then adding that to the PDF? Instead of taking that approach, I would highly recommend following the approach illustrated in this example:
https://github.com/tecnickcom/TCPDF/blob/master/examples/example_050.php
As you will see in the example, you should create an instance of the TCPDF class and then work with that instance, rather than calling the static methods.

How do I remove "hidden" characters when reading a line of text in Ruby?

I am using a custom Ruby function in Puppet to read a string of text from a file. I am than comparing whatever version is read against a list of known versions to determine which config file I should use for that particular server. The problem is that when I compare the read version to my list of known versions, none of them match.
I printed out the variable to the screen, and it looked fine. I then added a '-' to the beginning and the end and this time, the following was printed
-2.2#012-
Does anyone know what this is and how it could be removed?
Here is my process.
A script that handles the installation of an app
sudo echo "2.2" > /opt/version
My ruby function
if FileTest.exists?("/opt/version")
Facter.add("app_version") do
setcode do
version = File.open('/opt/version', &:readline)
version
end
end
end
My puppet manifest
if versioncmp( $app_version, '2.2') == 0 {
notice("===> Installing 2.2 Configs")
} elsif versioncmp ($app_version, '2.3') == 0 {
notice("===> Installing 2.3 Configs")
} else {
notice("===> No version match. Continuing on.")
}
}
File.readline includes the line termination (in your case, "\n"). chomp will get rid of the line termination:
version = File.open('/opt/version', &:readline).chomp
When debugging and you want to see what's really in a variable, use p instead of puts. p will escape unprintable characters so you can see them:
puts "2.2\n" # => 2.2
#
p "2.2\n" # => "2.2\n"

Ruby libxml: format XMLParser to expand closing tags [duplicate]

libxml2 (for C) is not preserving empty elements in their original form on a save. It replaces <tag></tag> with <tag/> which is technically correct but causes problems for us.
xmlDocPtr doc = xmlParseFile("myfile.xml");
xmlNodePtr root = xmlSaveFile("mynewfile.xml", doc);
I've tried playing with the various options (using xlmReadFile) but none seem to affect the output. One post here mentioned disabling tag compression but the example was for PERL and I've found no analog for C.
Is there an option to disable this behavior?
Just found this enum in the xmlsave module documentation:
Enum xmlSaveOption {
XML_SAVE_FORMAT = 1 : format save output
XML_SAVE_NO_DECL = 2 : drop the xml declaration
XML_SAVE_NO_EMPTY = 4 : no empty tags
XML_SAVE_NO_XHTML = 8 : disable XHTML1 specific rules
XML_SAVE_XHTML = 16 : force XHTML1 specific rules
XML_SAVE_AS_XML = 32 : force XML serialization on HTML doc
XML_SAVE_AS_HTML = 64 : force HTML serialization on XML doc
XML_SAVE_WSNONSIG = 128 : format with non-significant whitespace
}
Maybe you can refactor your application to use this module for serialization, and play a little with these options. Specially with XML_SAVE_NO_EMPTY.
Your code may look like this:
xmlSaveCtxt *ctxt = xmlSaveToFilename("mynewfile.xml", "UTF-8", XML_SAVE_FORMAT | XML_SAVE_NO_EMPTY);
if (!ctxt || xmlSaveDoc(ctxt, doc) < 0 || xmlSaveClose(ctxt) < 0)
//...deal with the error

Resources