html-conduit (HTML -> XHTML in Haskell) - haskell

I am a beginner to Haskell.
I have some code that uses xml-conduit, and it's all working well, I am able to transform it as I want.
let src_file = "blah.xhtml"
Document prologue root epilogue <- readFile def{psDecodeEntities=decodeHtmlEntities} src_file
let root' = transform root
-- And now we write out. Let's indent our output
writeFile def
{ rsPretty = True
} "output.xhtml" $ Document prologue root' epilogue
The problem I have is that my input comes from html (from the web). I want to feed this into my function. Currently to do this, I have to run the commandline utility 'tidy' to normalise it to XHTML:
tidy -output blah.xhtml -asxhtml blah.html
This is working but is not obviously not ideal. I don't want to write a commandline wrapper around 'tidy' and I am sure that this must have been solved already! I have found the html-conduit package, but couldn't work out how to connect one to the other.
If someone could shed some light about how to go about this, or another library that would do what I want, I would be grateful. I tried reading the documentation for html-conduit but couldn't work out how the two libraries interoperate.

You should be able to use the readFile function from html-conduit. What problems have you run into when trying to integrate the two libraries?

Related

Adding content in file in specific place with node js (Like Angular Cli Modify app.module file)

I got the idea that node js its not just for web application for example I can create a console application with node (cli) .
and already I have an interest in how I can make a cli app that create files and modify existing files for example something like angular cli with one command "ng generate component" its :-
1- create a set of files
2- modify app.module file
a. add import statement for generated component
b. add generated component in declarations array
and after a lot of search I got that first step can be handled in some way with node file system module.
but i don't know how they modify "app.module" file by just adding some syntax in its right place for instance adding new import statement after all exists import statements also adding the component name in declarations array as a last item
I'm really appreciate any help maybe with some code example if possible and thanks in advance
After some searches i found this answer:
There a couple of ways of editing a file, the most reliable is perhaps
the most complex one which can be done by parsing the file (Generating
an abstract syntax tree) update the new ast and pass it to a code
generator which will output the new string (code) of the modified ast.
Another option is to use regular expressions to know where add to
certain statements. For example there would be a regex to match import
statements to lines, you will map the lines of the file to this regex
where you'll get an array of booleans denoting whether the line is an
import stmt. Or not, once the import stmts are finished you can insert
a new line with the new import statement in the original lines array.
A third option is to regenerate the file all at once everytime, but
this means that you'll have to a ctx of the project (ctx = object
contains some details.
and as reference i found that AST (Abstract Syntax Tree) is more reliable way also it's not that hard this is some links that helped me a lot to know what is AST in simple way and how to deal with it
1- What is AST and how to understand it and how to use it https://www.youtube.com/watch?v=tM_S-pa4xDk
2- and this is an amazing article about "Write Code to Rewrite Your Code with jscodeshift" https://www.toptal.com/javascript/write-code-to-rewrite-your-code
https://www.youtube.com/watch?v=tM_S-pa4xDk

Change phpbb3.1 style to twig syntax

Recording to this, version 3.1 of phpBB should parse their old syntax to twig style now. I would like to use the parsed twig files to create a new style. I guess they can be found in the cache folder, but thats not very comfortable to reuse.
So Iam looking for the method that parses the old style to the knew one and to use it on the original files. I couldn't find it yet by just crawling through the sourcecode.
I found it myself. Its in the phpBB3/phpbb/template/twig/lexer.php file.
To use the class standalone, just remove
extends \Twig_Lexer
and replace
return parent::tokenize($code, $filename);
by
return $code;
Then you can run
$lexer = new lexer();
echo $lexer->tokenize($originalTemplateCode);
//returns template-code in twig-style syntax
Of course, thats a dirty hacked solution, but as long you only need it once to change the basic style for using it, its ok to do so

Can I alter Python source code while executing?

What I mean by this is:
I have a program. The end user is currently using it. I submit a new piece of source code and expect it to run as if it were always there?
I can't find an answer that specifically answers the point.
I'd like to be able to say, "extend" or add new features (rather than fix something that's already there on the fly) to the program without requiring a termination of the program (eg. Restart or exit).
Yes, you can definitely do that in python.
Although, it opens a security hole, so be very careful.
You can easily do this by setting up a "loader" class that can collect the source code you want it to use and then call the exec builtin function, just pass some python source code in and it will be evaluated.
Check the package
http://opensourcehacker.com/2011/11/08/sauna-reload-the-most-awesomely-named-python-package-ever/ . It allows to overcome certain raw edges of plain exec. Also it may be worth to check Dynamically reload a class definition in Python

Writing PDF binary file from stream yields malformed PDF

Dear Stack Overflow users,
I would appreciate you kind help with the following problem:
We have an Apache server functioning as a forward proxy, with ext_filter configured: whenever the response is of MIME type PDF, the filter is called (a perl script), and the PDF's content may be read from the STDIN. We read the PDF from STDIN, write it to a file and that's all. This almost always work well, but on one specific website, the PDF is malformed when written in the following way:
my $input_file = shift;
binmode STDIN;
open(OUT, ">" . $input_file);
binmode OUT;
foreach my $line (<STDIN>){
print OUT $line;
}
close OUT;
If we instead call 'tee' (set the filter to use 'tee')- the file is written correctly. Analyzing the malformed PDF shows that the xref table is malformed in the PDF we write and Adobe Reader fails to open it. We have already tried using sysopen,sysread etc. , using ":raw", and several other ways to write a binary file properly, and nothing worked (cut&paste code from documnetation for writing binary files). Only when using the 'tee' utility in linux as the filter, it was written correctly. This doesn't help us- we need to be able to write it to a file from stdin as part of the perl script. Any suggestions? If there could be a way to somehow call 'tee' with a system call, and give it STDIN of the perl program- it might could work. Many thanks in advance.
Well, although the code was basiclly correct, putting it inside "eval" somehow ruined thd PDF.
I still don't understand why, but deleting the eval solved the problem.
The perl is called from a context of ext_filter module of Apache.
I'll farther investigate this and update when I'll find an explanation for this.
Thanks for everyone.

Use global recursive from different functions in vimscript

I want to use multiple globals in different functions in vimscript but I get the following error:
"Cannot do :global recursive"
To my problem: I have a config file with paths to multiple XML-files. I want to use a global for every path in the config file and a global for every tag in each XML-File.
So I have some thing like this:
global search-for-a-file-path call functionX(filepath)
functionX(filepath)
edit filepath
global search-for-tags call functionThatDoesStuff()
functionThatDoesStuff()
Stuff happens here...
Is there a possibility to make this work with globals or do I need to use a different approach?
P.S.: I already saw this Q&A but it did not help me because I use the globals in different functions and the solution only shows how it is done in a "one-liner".
Vimscript does simply not seem to be able to do it, so I came up with the following solution:
while search("the-thing-I-search-for") > 0
call functionThatDoesStuff()
end while
My thanks go to Martin for help on the matter.

Resources