compiling .pm file for sending to webserver without creating a file #24

Closed
opened 4 years ago by somesayinice · 8 comments
somesayinice commented 4 years ago (Migrated from github.com)

Good afternoon. Just wondering if anyone else has tackled a similar problem. I'm trying to publish directly to a website using the website's api. I can connect successfully, get a response with authentication.

However, the data I've been sending is just junk data. What I'm trying to do now is raco pollen render without actually creating a file. I would like to output to a string which I can then send to the webserver for publishing or storage in sql or whatever. Does anyone have any ideas about how to output from a pollen markup file to a string?

I am thinking I should create a template.api file and do the work within the template file. Then create a poly-target called api. My only? problem, since ignorance is bliss, is I don't know how to stop the generation of a file.

Thanks much.

Good afternoon. Just wondering if anyone else has tackled a similar problem. I'm trying to publish directly to a website using the website's api. I can connect successfully, get a response with authentication. However, the data I've been sending is just junk data. What I'm trying to do now is raco pollen render without actually creating a file. I would like to output to a string which I can then send to the webserver for publishing or storage in sql or whatever. Does anyone have any ideas about how to output from a pollen markup file to a string? I am thinking I should create a *template.api* file and do the work within the template file. Then create a poly-target called api. My only? problem, since ignorance is bliss, is I don't know how to stop the generation of a file. Thanks much.
mbutterick commented 4 years ago (Migrated from github.com)

raco pollen render is just a convenience for using the underlying render function from pollen/render. You could call this directly with a source name to get a string.

`raco pollen render` is just a convenience for using the underlying [`render`](https://docs.racket-lang.org/pollen/Render.html?q=render#%28def._%28%28lib._pollen%2Frender..rkt%29._render%29%29) function from `pollen/render`. You could call this directly with a source name to get a string.
somesayinice commented 4 years ago (Migrated from github.com)

Thank you for the quick reply and for the insight. So right now I have four files: pollen.rkt, template.api, test_source.poly.pm, api_test.rkt. Runing the api_test.rkt allows me to get the string output I desire and of course I can send this output to the website api.

#lang racket
(require pollen/tag pollen/decode pollen/render pollen/setup txexpr hyphenate  pollen/file sugar pollen-tfl/pollen pollen/template)
(require racket/format racket/string racket/list racket/port racket/provide)
(require net/base64 2htdp/image net/uri-codec net/url net/http-client net/cgi json)

(define source-file (build-path (current-directory) "test_source.poly.pm"))
(current-poly-target 'api)
(render source-file)

But as you can see from the code, it is not dynamic so I'm not sure how to use it without manually editing this .rkt every single time. Ideally, I'd be able to place this in pollen.rkt so that I can build from sublime and it would process the .pm file and send the data to the website. But this doesn't seem to make any sense.

Thanks again for the help. I'll keep bashing along.

Thank you for the quick reply and for the insight. So right now I have four files: pollen.rkt, template.api, test_source.poly.pm, api_test.rkt. Runing the api_test.rkt allows me to get the string output I desire and of course I can send this output to the website api. ``` #lang racket (require pollen/tag pollen/decode pollen/render pollen/setup txexpr hyphenate pollen/file sugar pollen-tfl/pollen pollen/template) (require racket/format racket/string racket/list racket/port racket/provide) (require net/base64 2htdp/image net/uri-codec net/url net/http-client net/cgi json) (define source-file (build-path (current-directory) "test_source.poly.pm")) (current-poly-target 'api) (render source-file) ``` But as you can see from the code, it is not dynamic so I'm not sure how to use it without manually editing this .rkt every single time. Ideally, I'd be able to place this in pollen.rkt so that I can build from sublime and it would process the .pm file and send the data to the website. But this doesn't seem to make any sense. Thanks again for the help. I'll keep bashing along.
somesayinice commented 4 years ago (Migrated from github.com)

raco pollen render is just a convenience for using the underlying render function from pollen/render. You could call this directly with a source name to get a string.

I jury-rigged the dry run feature. I changed [wants-dry-run? (for-each message expanded-source-paths)] to [wants-dry-run? (for-each render expanded-source-paths)] in render.rkt. Now I can run raco pollen render -d -t api \"$file\" to output the string without generating the file and it fits in my workflow.

And it is also likely a very bad way to solve the problem. I am very open to learning how to do it better.

> `raco pollen render` is just a convenience for using the underlying [`render`](https://docs.racket-lang.org/pollen/Render.html?q=render#%28def._%28%28lib._pollen%2Frender..rkt%29._render%29%29) function from `pollen/render`. You could call this directly with a source name to get a string. I jury-rigged the dry run feature. I changed `[wants-dry-run? (for-each message expanded-source-paths)]` to `[wants-dry-run? (for-each render expanded-source-paths)]` in render.rkt. Now I can run `raco pollen render -d -t api \"$file\"` to output the string without generating the file and it fits in my workflow. And it is also likely a very bad way to solve the problem. I am very open to learning how to do it better.
mbutterick commented 4 years ago (Migrated from github.com)

I think I understand — you want to use raco pollen render to cause I/O side effects, but not leave behind any files. I suppose we could add a switch for this purpose. Is there a common terminology for this behavior in other command-line tools?

I think I understand — you want to use `raco pollen render` to cause I/O side effects, but not leave behind any files. I suppose we could add a switch for this purpose. Is there a common terminology for this behavior in other command-line tools?
somesayinice commented 4 years ago (Migrated from github.com)

That is exactly what I was trying to do. I'm not sure what the terminology is though with other command-line tools. But if dry-run means printing the name of the affected file, maybe a term like console-output might work? Thanks much again. Your help has made a big difference.

That is exactly what I was trying to do. I'm not sure what the terminology is though with other command-line tools. But if dry-run means printing the name of the affected file, maybe a term like console-output might work? Thanks much again. Your help has made a big difference.
mbutterick commented 4 years ago (Migrated from github.com)

I have added this option. It is called --null or -n and will suppress the file output of a render:

raco pollen render -n foo.html

I have added this option. It is called `--null` or `-n` and will suppress the file output of a render: `raco pollen render -n foo.html`
mbutterick commented 4 years ago (Migrated from github.com)

Added by fc5ca9f659

Added by https://github.com/mbutterick/pollen/commit/fc5ca9f65928a581b2545edf71a3201dab807883
somesayinice commented 4 years ago (Migrated from github.com)

I have added this option. It is called --null or -n and will suppress the file output of a render:

raco pollen render -n foo.html

Thanks so much!

> I have added this option. It is called `--null` or `-n` and will suppress the file output of a render: > > `raco pollen render -n foo.html` Thanks so much!
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#24
Loading…
There is no content yet.