I am writing a Twig node visitor and I think it is not reset between files and so I am wondering how could I easily detect when a new file is started.
It seems ModuleNode is always the first node for a parsed template, is this correct?
Yes, this is correct https://twig.symfony.com/doc/2.x/internals.html#the-parser says so:
The parser converts the token stream into an AST (Abstract Syntax Tree), or a node tree (an instance of \Twig\Node\ModuleNode).
Related
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
I have a File object and I want to pass it to PDF.js's getDocument method. On the frontend I would use the getDocument method like so:
pdfjs.getDocument(fileReader.readAsArrayBuffer(myFileHere))
How can I implement the above with Node.js? I am also using Express.js and Multer.
I have no idea whether PDF.js is able to run on Node, but let's assume it is.
At least with the readFile (or readFileSync if you prefer to be obtaining the contents "immediately") procedure available in Node, files may be read into Buffer objects, given the file's path.
If you take a look at the documentation of Buffer linked above, you'll also find specified that these are instances of UInt8Array:
The Buffer class is a subclass of JavaScript's Uint8Array class and extends it with methods that cover additional use cases.
Going further, an object of the UInt8Array class, also being an object of the TypedArray class (by the same specification that specifies the former), has a property named buffer, which lets you access the, well, underlying buffer1 that is... an ArrayBuffer!
So, you should be able to use a variation on the following, to pass an ArrayBuffer with some file's contents to PDF.js:
pdfjs.getDocument(require("fs").readFileSync("/path/to/file").buffer);
I have to assume you must read the file by path -- I assume your fileReader is a FileReader, which (contrary to its misleading name) reads Blob objects (and not files) -- and Node does not have any Blob class defined! So if you don't want to read the file by some path, then you'll have to tell us how you load file contents with Node at all.
1 Don't blame me for the word "buffer" all over here, it's buffers all the way down, apparently -- I didn't design any of these classes ;)
I would like to wrap a data file (~1MB) to golang app and then use that data in os.exec. The app runs on Linux.
How to define the data in the app, as a string or []byte, variable, or Const?
Should be defined in a global scope, or wrapped in a func?
How to pass the data from the app memory to the executed process ?
For building the data file(s) in to your program, you have a number of choices. You are correct in that you could manually copy/paste the data file(s) in to the program as types string, []byte as variables, but there are other packages/applications for go that handle this for you already that can also minimize your app's memory footprint. One example that comes to mind is go-bindata (link to repo) which does just this.
As for using the file in os/exec, I'm assuming you're trying to either pass the entire file to the program using either a file path or a raw string. For file paths, you would have to write the entire file to disk first. If you're trying to pass the entire file contents as a raw string, you can still use go-bindata or a string of the data file as arg in os/exec.Command.
P.S. - go-bindata has not seen updates in a while, so I would encourage you to seek more active forks or alternatives if you're having trouble using it.
For example,
router.get('/files/...', ->) I want to match /files/a, /files/a/b, /files/a/b/c and so on.
Then, a, a/b, a/b/c will be my handle file path.
This should work for the path, using a regex instead of a string: /\/files\/(.*)/ -- then the path info should be in this.params[0].
Side note -- if you're using this to serve files directly from the server, make sure to check for a directory traversal attack (often looks like using .. in the requested path multiple times) so that you don't inadvertently expose other files on the system.
I have previously written the following command in PHP in order to merge two fies using php imagick.
$canvasImg->compositeImage($overlayImg, Imagick::COMPOSITE_OVER, $x, $y);
Now I'm trying to do the same using nodejs GraphicsMagic. But the gm.composite('path.jpg', callback) only accepts file paths. I have created a pretty complex image that I have in memory that I want to use like the example in PHP, so saving it to disk in order to merge it is not an option (I'm using it in a nested loop so there can be a lot of mergings).
So, is there any way of using the objects from memory and composite them together?