ConTeXt, HTML source code side by side to the result from the web
up vote
5
down vote
favorite
I would like to write a document with some HTML code and next to it the result from a webpage.
Basically something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
Would print the code highlighted side by side to the result of how it looks in a website.
What I have in mind is that the environment
- creates a new file for each example with .html extension where it stores some code that goes before the contents of the environment;
- then the contents of the environment, in this case
<strong>Hello</strong>, <em>world<em>; - some post html tags to close the file;
then process the file with a command
firefox -screenshot "screenshot-n.png" "file:///Users/blah/blah/file-n.html"
(well, with options
--window-size="certainwidth,certainheight") to get a .png
then add side by side the code (colored with
usemodule[vim]) and the image of the results.
Here's a “mockup”
starttext
Something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
should do something like
% here v pre code
startbuffer
<html>
<strong>Hello</strong>, <em>world<em>
</html>
stopbuffer % here ^ post code
% save this buffer to a file, say, htmls/html-1.html
% take a screenshot and save to screenshots/screenshot-1.png
startxtable[option=stretch]
startxrow
startxcell
typehtmlfile[htmls/html-1.html] % this seems buggy
stopxcell
startxcell
externalfile[screenshots/screenshot-1.png]
stopxcell
stopxrow
stopxtable
stoptext
And to be clear, I'm relatively new to ConTeXt, so this might be solvable with other systems, and this is an X Y problem.
My idea is to write a document with many small examples of html code.
context html
add a comment |
up vote
5
down vote
favorite
I would like to write a document with some HTML code and next to it the result from a webpage.
Basically something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
Would print the code highlighted side by side to the result of how it looks in a website.
What I have in mind is that the environment
- creates a new file for each example with .html extension where it stores some code that goes before the contents of the environment;
- then the contents of the environment, in this case
<strong>Hello</strong>, <em>world<em>; - some post html tags to close the file;
then process the file with a command
firefox -screenshot "screenshot-n.png" "file:///Users/blah/blah/file-n.html"
(well, with options
--window-size="certainwidth,certainheight") to get a .png
then add side by side the code (colored with
usemodule[vim]) and the image of the results.
Here's a “mockup”
starttext
Something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
should do something like
% here v pre code
startbuffer
<html>
<strong>Hello</strong>, <em>world<em>
</html>
stopbuffer % here ^ post code
% save this buffer to a file, say, htmls/html-1.html
% take a screenshot and save to screenshots/screenshot-1.png
startxtable[option=stretch]
startxrow
startxcell
typehtmlfile[htmls/html-1.html] % this seems buggy
stopxcell
startxcell
externalfile[screenshots/screenshot-1.png]
stopxcell
stopxrow
stopxtable
stoptext
And to be clear, I'm relatively new to ConTeXt, so this might be solvable with other systems, and this is an X Y problem.
My idea is to write a document with many small examples of html code.
context html
3
Looks like a job forfilter.
– Henri Menke
Nov 16 at 20:38
Thanks, yes, it looks like it :) Not able to make it work, but working on it. Nice to see this things exist.
– Manuel
Nov 16 at 21:44
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I would like to write a document with some HTML code and next to it the result from a webpage.
Basically something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
Would print the code highlighted side by side to the result of how it looks in a website.
What I have in mind is that the environment
- creates a new file for each example with .html extension where it stores some code that goes before the contents of the environment;
- then the contents of the environment, in this case
<strong>Hello</strong>, <em>world<em>; - some post html tags to close the file;
then process the file with a command
firefox -screenshot "screenshot-n.png" "file:///Users/blah/blah/file-n.html"
(well, with options
--window-size="certainwidth,certainheight") to get a .png
then add side by side the code (colored with
usemodule[vim]) and the image of the results.
Here's a “mockup”
starttext
Something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
should do something like
% here v pre code
startbuffer
<html>
<strong>Hello</strong>, <em>world<em>
</html>
stopbuffer % here ^ post code
% save this buffer to a file, say, htmls/html-1.html
% take a screenshot and save to screenshots/screenshot-1.png
startxtable[option=stretch]
startxrow
startxcell
typehtmlfile[htmls/html-1.html] % this seems buggy
stopxcell
startxcell
externalfile[screenshots/screenshot-1.png]
stopxcell
stopxrow
stopxtable
stoptext
And to be clear, I'm relatively new to ConTeXt, so this might be solvable with other systems, and this is an X Y problem.
My idea is to write a document with many small examples of html code.
context html
I would like to write a document with some HTML code and next to it the result from a webpage.
Basically something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
Would print the code highlighted side by side to the result of how it looks in a website.
What I have in mind is that the environment
- creates a new file for each example with .html extension where it stores some code that goes before the contents of the environment;
- then the contents of the environment, in this case
<strong>Hello</strong>, <em>world<em>; - some post html tags to close the file;
then process the file with a command
firefox -screenshot "screenshot-n.png" "file:///Users/blah/blah/file-n.html"
(well, with options
--window-size="certainwidth,certainheight") to get a .png
then add side by side the code (colored with
usemodule[vim]) and the image of the results.
Here's a “mockup”
starttext
Something like
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
should do something like
% here v pre code
startbuffer
<html>
<strong>Hello</strong>, <em>world<em>
</html>
stopbuffer % here ^ post code
% save this buffer to a file, say, htmls/html-1.html
% take a screenshot and save to screenshots/screenshot-1.png
startxtable[option=stretch]
startxrow
startxcell
typehtmlfile[htmls/html-1.html] % this seems buggy
stopxcell
startxcell
externalfile[screenshots/screenshot-1.png]
stopxcell
stopxrow
stopxtable
stoptext
And to be clear, I'm relatively new to ConTeXt, so this might be solvable with other systems, and this is an X Y problem.
My idea is to write a document with many small examples of html code.
context html
context html
edited Nov 17 at 14:02
asked Nov 16 at 19:39
Manuel
21k845105
21k845105
3
Looks like a job forfilter.
– Henri Menke
Nov 16 at 20:38
Thanks, yes, it looks like it :) Not able to make it work, but working on it. Nice to see this things exist.
– Manuel
Nov 16 at 21:44
add a comment |
3
Looks like a job forfilter.
– Henri Menke
Nov 16 at 20:38
Thanks, yes, it looks like it :) Not able to make it work, but working on it. Nice to see this things exist.
– Manuel
Nov 16 at 21:44
3
3
Looks like a job for
filter.– Henri Menke
Nov 16 at 20:38
Looks like a job for
filter.– Henri Menke
Nov 16 at 20:38
Thanks, yes, it looks like it :) Not able to make it work, but working on it. Nice to see this things exist.
– Manuel
Nov 16 at 21:44
Thanks, yes, it looks like it :) Not able to make it work, but working on it. Nice to see this things exist.
– Manuel
Nov 16 at 21:44
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
I cannot install webkit2png, so here is an illustration of how to do this using pandoc to convert html to pdf. First, let's start with the basic feature of showing both the source. Note that you need a local directory call html for this to work.
usemodule[filter]
startbuffer[before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
startbuffer[after-html-example]
</body>
</html>
stopbuffer
traceexternalfilters
defineexternalfilter
[htmlexample]
[
directory=html,
bufferbefore=before-html-example,
bufferafter=after-html-example,
output=externalfilterbasefile.pdf,
filtercommand={pandoc -f html externalfilterinputfilespace -o externalfilteroutputfile},
purge=no,
cache=yes,
readcommand=DisplaySourceAndOutput,
]
defDisplaySourceAndOutput#1%
{typefile{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
starttext
Hello
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
stoptext

Now, let's add syntax highlighting and remove the before and after code from the displayed code snippet. So, we change the definition of DisplaySourceAndOutput as follows:
usemodule[vim]
definevimtyping[html][syntax=html,directory=html]
defDisplaySourceAndOutput#1%
{typehtmlfile[start=6,stop=-2]{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
The value start=6 asks typehtmlfile to show the output from the 6th line (because before-html-example has 5 lines). The value stop=-2 asks not to show the last 2 lines (because after-html-example has 2 lines).
The above example now gives:

Now all you need to do is modify DisplaySourceAndOutput as you want it to be displayed.
EDIT
I don't understand what you mean by the comment:
For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexampleor something like that.
Assuming that you want to ignore part of the environment, that is actually relatively easy (if you use buffers). For example, here is code to delete everything before the string !!START!! in the buffer:
startluacode
filterdata = filterdata or {}
local ctxcatcodes = catcodes.numbers.ctxcatcodes
filterdata.pre_process = function(name)
local content = buffers.getcontent(name)
content = string.gsub(content,'^.*!!START!!',"")
buffers.assign(name,content, ctxcatcodes)
end
stopluacode
starttext
Hello
startbuffer[demo]
Stuff which is ignored
!!START!!
Stuff which is not ignored
stopbuffer
ctxlua{filterdata.pre_process("demo")}
typebuffer[demo]
stoptext
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow forstarthtmlexample ... realhtmlexample ... stophtmlexampleor something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?
– Manuel
Nov 18 at 21:52
xtabledoes not work inside a macro. You can try the simplerplacesidebyside{...}{...}or natural tables. If you really need the features ofxtables, then usingstartsetups ... stopsetupsshould probably work.
– Aditya
Nov 19 at 4:34
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour asstrip=yesafter that!!START!!.
– Manuel
Nov 19 at 8:23
Simply useprocesshtmlexamplebuffer[demo].
– Aditya
Nov 19 at 13:49
add a comment |
up vote
3
down vote
This is an answer as a work-in-progress of what I'm trying to achieve, with the suggestion of Henri Menke of using filter module, in company with webkit2png.
At the moment this “just works”. But it's far from the definitive. I'll leave it here until (hopefully) someone writes a better and more complete answer.
Things that I hope I can change:
- save every extra file (from
filter,vim, andwebkit2png) in anfolder/so that the current directory is not polluted;
This one is solved through some hacks in the bash commands.
have some “pre” and “post” code so one does not need to write everything (for example now I need to write<html><body>etc.), ideally the input would be for example
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
manage correctly the sizes of the image, and the fonts, and everything. Right now it's very fragile (I've done by hand the adjust of
font-size: 30in the webpage).
add some options to configure each example individually:
- for example if we want examples side by side (for small examples), something like
starthtmlexample[sidebyside]orstarthtmlexample[sidebyside=30]or something like that to say what part is for the code and which part for the image, and also another option to show first the code then below the result; - options to change the pre-code, and post-code for an individual piece;
some way of altering what's shown and what's written, for example
starthmlexample
<p> Lorem ipsum ... </p>
realhtml
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
stophtmlexample
- for example if we want examples side by side (for small examples), something like
an inline way of doing this that ends up working in kind of tables:
startxtable
startxrow
inlinehtmlexample{<b>bold</b>}
stopxrow
sartxrow
inlinehtmlexample{<i>cursive</i>}
stopxrow
stopxtable
to align many small results one below another.
right now I can't remember more things I thought (the
frame=onis jut to know what's happening at the moment, that I don't actually know many things I'm using here); and of courseget a decent base design so that it looks better than the default I wrote.
Here's the code:
usemodule[vim]
definevimtyping
[html]
[syntax=html,
directory=auxfiles,
before={blanksetupbodyfont[small]},
after={blank},
lines=split]
startbuffer [before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
savebuffer [before-html-example] [before-html-example]
% startbuffer [left-html-example]
%
% stopbuffer
%
% startbuffer [right-html-example]
%
% stopbuffer
startbuffer [after-html-example]
</body>
</html>
stopbuffer
savebuffer [after-html-example] [after-html-example]
usemodule[filter]
defineexternalfilter
[htmlexample]
[%directory=auxfiles, % this doesn't work as (I) expected
% input=externalfilterbasefile.html, % this doesn't exist, but may be should?
output=externalfilterbasefile-full.png, % because of how webkit2png works
filtercommand=htmlfiltercommand,
readcommand=htmlexamplereadcommand,
cache=yes,
indentnext=no]
defhtmlexamplereadcommand#1%
{bpartypehtmlfile{externalfilterinputfile}epar
bparexternalfigure[#1][frame=on,maxwidth=.5textwidth]epar}
deffirefoxpath{/Applications/Firefox.app/Contents/MacOS}
defchromepath{/Applications/Google Chrome.app/Contents/MacOS}
defhtmlfiltercommand
{htmlconcatenatepartshtmlobtainpng}
defhtmlconcatenateparts
{cat "html-before-html-example.tmp" "externalfilterinputfile" "html-after-html-example.tmp" > "tmp.html";}
defhtmlobtainpng
{webkit2png
%--dir="auxfiles" % doesn't work correctly with externalfilter
--fullsize
--width=1 % to get the minimum
--height=1 % to get the minimum
--scale=1 % unused
--filename="externalfilterbasefile"
"tmp.html";}
% {firefoxpath/firefox
% -screenshot
% "externalfilteroutputfile"
% "externalfilterinputfile"}
% {chromepath/Google Chrome
% --headless
% --screenshot="externalfilteroutputfile"
% "externalfilterinputfile"}
starttext
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<pre>
1 2 3 4 5 6 7 <b>Forever,</b><br/>
8 9 10 11 12 13 14 and ever.<br/>
15 16 17 18 19 20 21
</pre>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure inlinehtml{<b>alo</b>} dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
stopchapter
stoptext
Fordirectory=outputto work, you need to create the directory on your own.
– Aditya
Nov 18 at 7:07
For adding before and after content, search forbufferbeforeandbufferafterin the documentation of the filter module
– Aditya
Nov 18 at 7:13
I see, still it doesn't work entirely well (or as expected) withwebkit2pngso I decided not to use it. Andbufferbeforeandbufferafteris of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.
– Manuel
Nov 18 at 13:07
Doeswebkit2png --filename=html/test.tmp html/test.pngwork from the command line? If so, then it should also work from within the filter module. Regardingbufferbeforretc, I don't understand what you mean by print those things while processing a different one. Can you give an example?
– Aditya
Nov 18 at 14:28
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I writestarthtmlexample <b>bold</bold> stophtmlexampleI want the file to be processed to have<html><body><b>bold</b></body></html>for example.
– Manuel
Nov 18 at 14:56
|
show 3 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I cannot install webkit2png, so here is an illustration of how to do this using pandoc to convert html to pdf. First, let's start with the basic feature of showing both the source. Note that you need a local directory call html for this to work.
usemodule[filter]
startbuffer[before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
startbuffer[after-html-example]
</body>
</html>
stopbuffer
traceexternalfilters
defineexternalfilter
[htmlexample]
[
directory=html,
bufferbefore=before-html-example,
bufferafter=after-html-example,
output=externalfilterbasefile.pdf,
filtercommand={pandoc -f html externalfilterinputfilespace -o externalfilteroutputfile},
purge=no,
cache=yes,
readcommand=DisplaySourceAndOutput,
]
defDisplaySourceAndOutput#1%
{typefile{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
starttext
Hello
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
stoptext

Now, let's add syntax highlighting and remove the before and after code from the displayed code snippet. So, we change the definition of DisplaySourceAndOutput as follows:
usemodule[vim]
definevimtyping[html][syntax=html,directory=html]
defDisplaySourceAndOutput#1%
{typehtmlfile[start=6,stop=-2]{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
The value start=6 asks typehtmlfile to show the output from the 6th line (because before-html-example has 5 lines). The value stop=-2 asks not to show the last 2 lines (because after-html-example has 2 lines).
The above example now gives:

Now all you need to do is modify DisplaySourceAndOutput as you want it to be displayed.
EDIT
I don't understand what you mean by the comment:
For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexampleor something like that.
Assuming that you want to ignore part of the environment, that is actually relatively easy (if you use buffers). For example, here is code to delete everything before the string !!START!! in the buffer:
startluacode
filterdata = filterdata or {}
local ctxcatcodes = catcodes.numbers.ctxcatcodes
filterdata.pre_process = function(name)
local content = buffers.getcontent(name)
content = string.gsub(content,'^.*!!START!!',"")
buffers.assign(name,content, ctxcatcodes)
end
stopluacode
starttext
Hello
startbuffer[demo]
Stuff which is ignored
!!START!!
Stuff which is not ignored
stopbuffer
ctxlua{filterdata.pre_process("demo")}
typebuffer[demo]
stoptext
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow forstarthtmlexample ... realhtmlexample ... stophtmlexampleor something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?
– Manuel
Nov 18 at 21:52
xtabledoes not work inside a macro. You can try the simplerplacesidebyside{...}{...}or natural tables. If you really need the features ofxtables, then usingstartsetups ... stopsetupsshould probably work.
– Aditya
Nov 19 at 4:34
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour asstrip=yesafter that!!START!!.
– Manuel
Nov 19 at 8:23
Simply useprocesshtmlexamplebuffer[demo].
– Aditya
Nov 19 at 13:49
add a comment |
up vote
2
down vote
accepted
I cannot install webkit2png, so here is an illustration of how to do this using pandoc to convert html to pdf. First, let's start with the basic feature of showing both the source. Note that you need a local directory call html for this to work.
usemodule[filter]
startbuffer[before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
startbuffer[after-html-example]
</body>
</html>
stopbuffer
traceexternalfilters
defineexternalfilter
[htmlexample]
[
directory=html,
bufferbefore=before-html-example,
bufferafter=after-html-example,
output=externalfilterbasefile.pdf,
filtercommand={pandoc -f html externalfilterinputfilespace -o externalfilteroutputfile},
purge=no,
cache=yes,
readcommand=DisplaySourceAndOutput,
]
defDisplaySourceAndOutput#1%
{typefile{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
starttext
Hello
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
stoptext

Now, let's add syntax highlighting and remove the before and after code from the displayed code snippet. So, we change the definition of DisplaySourceAndOutput as follows:
usemodule[vim]
definevimtyping[html][syntax=html,directory=html]
defDisplaySourceAndOutput#1%
{typehtmlfile[start=6,stop=-2]{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
The value start=6 asks typehtmlfile to show the output from the 6th line (because before-html-example has 5 lines). The value stop=-2 asks not to show the last 2 lines (because after-html-example has 2 lines).
The above example now gives:

Now all you need to do is modify DisplaySourceAndOutput as you want it to be displayed.
EDIT
I don't understand what you mean by the comment:
For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexampleor something like that.
Assuming that you want to ignore part of the environment, that is actually relatively easy (if you use buffers). For example, here is code to delete everything before the string !!START!! in the buffer:
startluacode
filterdata = filterdata or {}
local ctxcatcodes = catcodes.numbers.ctxcatcodes
filterdata.pre_process = function(name)
local content = buffers.getcontent(name)
content = string.gsub(content,'^.*!!START!!',"")
buffers.assign(name,content, ctxcatcodes)
end
stopluacode
starttext
Hello
startbuffer[demo]
Stuff which is ignored
!!START!!
Stuff which is not ignored
stopbuffer
ctxlua{filterdata.pre_process("demo")}
typebuffer[demo]
stoptext
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow forstarthtmlexample ... realhtmlexample ... stophtmlexampleor something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?
– Manuel
Nov 18 at 21:52
xtabledoes not work inside a macro. You can try the simplerplacesidebyside{...}{...}or natural tables. If you really need the features ofxtables, then usingstartsetups ... stopsetupsshould probably work.
– Aditya
Nov 19 at 4:34
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour asstrip=yesafter that!!START!!.
– Manuel
Nov 19 at 8:23
Simply useprocesshtmlexamplebuffer[demo].
– Aditya
Nov 19 at 13:49
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I cannot install webkit2png, so here is an illustration of how to do this using pandoc to convert html to pdf. First, let's start with the basic feature of showing both the source. Note that you need a local directory call html for this to work.
usemodule[filter]
startbuffer[before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
startbuffer[after-html-example]
</body>
</html>
stopbuffer
traceexternalfilters
defineexternalfilter
[htmlexample]
[
directory=html,
bufferbefore=before-html-example,
bufferafter=after-html-example,
output=externalfilterbasefile.pdf,
filtercommand={pandoc -f html externalfilterinputfilespace -o externalfilteroutputfile},
purge=no,
cache=yes,
readcommand=DisplaySourceAndOutput,
]
defDisplaySourceAndOutput#1%
{typefile{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
starttext
Hello
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
stoptext

Now, let's add syntax highlighting and remove the before and after code from the displayed code snippet. So, we change the definition of DisplaySourceAndOutput as follows:
usemodule[vim]
definevimtyping[html][syntax=html,directory=html]
defDisplaySourceAndOutput#1%
{typehtmlfile[start=6,stop=-2]{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
The value start=6 asks typehtmlfile to show the output from the 6th line (because before-html-example has 5 lines). The value stop=-2 asks not to show the last 2 lines (because after-html-example has 2 lines).
The above example now gives:

Now all you need to do is modify DisplaySourceAndOutput as you want it to be displayed.
EDIT
I don't understand what you mean by the comment:
For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexampleor something like that.
Assuming that you want to ignore part of the environment, that is actually relatively easy (if you use buffers). For example, here is code to delete everything before the string !!START!! in the buffer:
startluacode
filterdata = filterdata or {}
local ctxcatcodes = catcodes.numbers.ctxcatcodes
filterdata.pre_process = function(name)
local content = buffers.getcontent(name)
content = string.gsub(content,'^.*!!START!!',"")
buffers.assign(name,content, ctxcatcodes)
end
stopluacode
starttext
Hello
startbuffer[demo]
Stuff which is ignored
!!START!!
Stuff which is not ignored
stopbuffer
ctxlua{filterdata.pre_process("demo")}
typebuffer[demo]
stoptext
I cannot install webkit2png, so here is an illustration of how to do this using pandoc to convert html to pdf. First, let's start with the basic feature of showing both the source. Note that you need a local directory call html for this to work.
usemodule[filter]
startbuffer[before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
startbuffer[after-html-example]
</body>
</html>
stopbuffer
traceexternalfilters
defineexternalfilter
[htmlexample]
[
directory=html,
bufferbefore=before-html-example,
bufferafter=after-html-example,
output=externalfilterbasefile.pdf,
filtercommand={pandoc -f html externalfilterinputfilespace -o externalfilteroutputfile},
purge=no,
cache=yes,
readcommand=DisplaySourceAndOutput,
]
defDisplaySourceAndOutput#1%
{typefile{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
starttext
Hello
starthtmlexample
<strong>Hello</strong>, <em>world<em>
stophtmlexample
stoptext

Now, let's add syntax highlighting and remove the before and after code from the displayed code snippet. So, we change the definition of DisplaySourceAndOutput as follows:
usemodule[vim]
definevimtyping[html][syntax=html,directory=html]
defDisplaySourceAndOutput#1%
{typehtmlfile[start=6,stop=-2]{externalfilterinputfile}
externalfigure[#1][frame=on,width=10cm]}
The value start=6 asks typehtmlfile to show the output from the 6th line (because before-html-example has 5 lines). The value stop=-2 asks not to show the last 2 lines (because after-html-example has 2 lines).
The above example now gives:

Now all you need to do is modify DisplaySourceAndOutput as you want it to be displayed.
EDIT
I don't understand what you mean by the comment:
For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexampleor something like that.
Assuming that you want to ignore part of the environment, that is actually relatively easy (if you use buffers). For example, here is code to delete everything before the string !!START!! in the buffer:
startluacode
filterdata = filterdata or {}
local ctxcatcodes = catcodes.numbers.ctxcatcodes
filterdata.pre_process = function(name)
local content = buffers.getcontent(name)
content = string.gsub(content,'^.*!!START!!',"")
buffers.assign(name,content, ctxcatcodes)
end
stopluacode
starttext
Hello
startbuffer[demo]
Stuff which is ignored
!!START!!
Stuff which is not ignored
stopbuffer
ctxlua{filterdata.pre_process("demo")}
typebuffer[demo]
stoptext
edited Nov 19 at 4:30
answered Nov 18 at 16:57
Aditya
54.7k2109233
54.7k2109233
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow forstarthtmlexample ... realhtmlexample ... stophtmlexampleor something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?
– Manuel
Nov 18 at 21:52
xtabledoes not work inside a macro. You can try the simplerplacesidebyside{...}{...}or natural tables. If you really need the features ofxtables, then usingstartsetups ... stopsetupsshould probably work.
– Aditya
Nov 19 at 4:34
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour asstrip=yesafter that!!START!!.
– Manuel
Nov 19 at 8:23
Simply useprocesshtmlexamplebuffer[demo].
– Aditya
Nov 19 at 13:49
add a comment |
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow forstarthtmlexample ... realhtmlexample ... stophtmlexampleor something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?
– Manuel
Nov 18 at 21:52
xtabledoes not work inside a macro. You can try the simplerplacesidebyside{...}{...}or natural tables. If you really need the features ofxtables, then usingstartsetups ... stopsetupsshould probably work.
– Aditya
Nov 19 at 4:34
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour asstrip=yesafter that!!START!!.
– Manuel
Nov 19 at 8:23
Simply useprocesshtmlexamplebuffer[demo].
– Aditya
Nov 19 at 13:49
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexample or something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?– Manuel
Nov 18 at 21:52
Okey, right now this works. Still it would be nice if there was a way to pre-process the buffer contents inside tex before exporting and processing with the filtercommand. For example to allow for
starthtmlexample ... realhtmlexample ... stophtmlexample or something like that. Also, is there a way to output the contents of the code like in a table, with some cell for the text and then a second cell in the same row to put the image?– Manuel
Nov 18 at 21:52
xtable does not work inside a macro. You can try the simpler placesidebyside{...}{...} or natural tables. If you really need the features of xtables, then using startsetups ... stopsetups should probably work.– Aditya
Nov 19 at 4:34
xtable does not work inside a macro. You can try the simpler placesidebyside{...}{...} or natural tables. If you really need the features of xtables, then using startsetups ... stopsetups should probably work.– Aditya
Nov 19 at 4:34
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour as
strip=yes after that !!START!!.– Manuel
Nov 19 at 8:23
And how could I hook that lua code before filter saves the buffer to a file? Also I would love if I could have the same behaviour as
strip=yes after that !!START!!.– Manuel
Nov 19 at 8:23
Simply use
processhtmlexamplebuffer[demo].– Aditya
Nov 19 at 13:49
Simply use
processhtmlexamplebuffer[demo].– Aditya
Nov 19 at 13:49
add a comment |
up vote
3
down vote
This is an answer as a work-in-progress of what I'm trying to achieve, with the suggestion of Henri Menke of using filter module, in company with webkit2png.
At the moment this “just works”. But it's far from the definitive. I'll leave it here until (hopefully) someone writes a better and more complete answer.
Things that I hope I can change:
- save every extra file (from
filter,vim, andwebkit2png) in anfolder/so that the current directory is not polluted;
This one is solved through some hacks in the bash commands.
have some “pre” and “post” code so one does not need to write everything (for example now I need to write<html><body>etc.), ideally the input would be for example
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
manage correctly the sizes of the image, and the fonts, and everything. Right now it's very fragile (I've done by hand the adjust of
font-size: 30in the webpage).
add some options to configure each example individually:
- for example if we want examples side by side (for small examples), something like
starthtmlexample[sidebyside]orstarthtmlexample[sidebyside=30]or something like that to say what part is for the code and which part for the image, and also another option to show first the code then below the result; - options to change the pre-code, and post-code for an individual piece;
some way of altering what's shown and what's written, for example
starthmlexample
<p> Lorem ipsum ... </p>
realhtml
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
stophtmlexample
- for example if we want examples side by side (for small examples), something like
an inline way of doing this that ends up working in kind of tables:
startxtable
startxrow
inlinehtmlexample{<b>bold</b>}
stopxrow
sartxrow
inlinehtmlexample{<i>cursive</i>}
stopxrow
stopxtable
to align many small results one below another.
right now I can't remember more things I thought (the
frame=onis jut to know what's happening at the moment, that I don't actually know many things I'm using here); and of courseget a decent base design so that it looks better than the default I wrote.
Here's the code:
usemodule[vim]
definevimtyping
[html]
[syntax=html,
directory=auxfiles,
before={blanksetupbodyfont[small]},
after={blank},
lines=split]
startbuffer [before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
savebuffer [before-html-example] [before-html-example]
% startbuffer [left-html-example]
%
% stopbuffer
%
% startbuffer [right-html-example]
%
% stopbuffer
startbuffer [after-html-example]
</body>
</html>
stopbuffer
savebuffer [after-html-example] [after-html-example]
usemodule[filter]
defineexternalfilter
[htmlexample]
[%directory=auxfiles, % this doesn't work as (I) expected
% input=externalfilterbasefile.html, % this doesn't exist, but may be should?
output=externalfilterbasefile-full.png, % because of how webkit2png works
filtercommand=htmlfiltercommand,
readcommand=htmlexamplereadcommand,
cache=yes,
indentnext=no]
defhtmlexamplereadcommand#1%
{bpartypehtmlfile{externalfilterinputfile}epar
bparexternalfigure[#1][frame=on,maxwidth=.5textwidth]epar}
deffirefoxpath{/Applications/Firefox.app/Contents/MacOS}
defchromepath{/Applications/Google Chrome.app/Contents/MacOS}
defhtmlfiltercommand
{htmlconcatenatepartshtmlobtainpng}
defhtmlconcatenateparts
{cat "html-before-html-example.tmp" "externalfilterinputfile" "html-after-html-example.tmp" > "tmp.html";}
defhtmlobtainpng
{webkit2png
%--dir="auxfiles" % doesn't work correctly with externalfilter
--fullsize
--width=1 % to get the minimum
--height=1 % to get the minimum
--scale=1 % unused
--filename="externalfilterbasefile"
"tmp.html";}
% {firefoxpath/firefox
% -screenshot
% "externalfilteroutputfile"
% "externalfilterinputfile"}
% {chromepath/Google Chrome
% --headless
% --screenshot="externalfilteroutputfile"
% "externalfilterinputfile"}
starttext
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<pre>
1 2 3 4 5 6 7 <b>Forever,</b><br/>
8 9 10 11 12 13 14 and ever.<br/>
15 16 17 18 19 20 21
</pre>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure inlinehtml{<b>alo</b>} dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
stopchapter
stoptext
Fordirectory=outputto work, you need to create the directory on your own.
– Aditya
Nov 18 at 7:07
For adding before and after content, search forbufferbeforeandbufferafterin the documentation of the filter module
– Aditya
Nov 18 at 7:13
I see, still it doesn't work entirely well (or as expected) withwebkit2pngso I decided not to use it. Andbufferbeforeandbufferafteris of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.
– Manuel
Nov 18 at 13:07
Doeswebkit2png --filename=html/test.tmp html/test.pngwork from the command line? If so, then it should also work from within the filter module. Regardingbufferbeforretc, I don't understand what you mean by print those things while processing a different one. Can you give an example?
– Aditya
Nov 18 at 14:28
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I writestarthtmlexample <b>bold</bold> stophtmlexampleI want the file to be processed to have<html><body><b>bold</b></body></html>for example.
– Manuel
Nov 18 at 14:56
|
show 3 more comments
up vote
3
down vote
This is an answer as a work-in-progress of what I'm trying to achieve, with the suggestion of Henri Menke of using filter module, in company with webkit2png.
At the moment this “just works”. But it's far from the definitive. I'll leave it here until (hopefully) someone writes a better and more complete answer.
Things that I hope I can change:
- save every extra file (from
filter,vim, andwebkit2png) in anfolder/so that the current directory is not polluted;
This one is solved through some hacks in the bash commands.
have some “pre” and “post” code so one does not need to write everything (for example now I need to write<html><body>etc.), ideally the input would be for example
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
manage correctly the sizes of the image, and the fonts, and everything. Right now it's very fragile (I've done by hand the adjust of
font-size: 30in the webpage).
add some options to configure each example individually:
- for example if we want examples side by side (for small examples), something like
starthtmlexample[sidebyside]orstarthtmlexample[sidebyside=30]or something like that to say what part is for the code and which part for the image, and also another option to show first the code then below the result; - options to change the pre-code, and post-code for an individual piece;
some way of altering what's shown and what's written, for example
starthmlexample
<p> Lorem ipsum ... </p>
realhtml
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
stophtmlexample
- for example if we want examples side by side (for small examples), something like
an inline way of doing this that ends up working in kind of tables:
startxtable
startxrow
inlinehtmlexample{<b>bold</b>}
stopxrow
sartxrow
inlinehtmlexample{<i>cursive</i>}
stopxrow
stopxtable
to align many small results one below another.
right now I can't remember more things I thought (the
frame=onis jut to know what's happening at the moment, that I don't actually know many things I'm using here); and of courseget a decent base design so that it looks better than the default I wrote.
Here's the code:
usemodule[vim]
definevimtyping
[html]
[syntax=html,
directory=auxfiles,
before={blanksetupbodyfont[small]},
after={blank},
lines=split]
startbuffer [before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
savebuffer [before-html-example] [before-html-example]
% startbuffer [left-html-example]
%
% stopbuffer
%
% startbuffer [right-html-example]
%
% stopbuffer
startbuffer [after-html-example]
</body>
</html>
stopbuffer
savebuffer [after-html-example] [after-html-example]
usemodule[filter]
defineexternalfilter
[htmlexample]
[%directory=auxfiles, % this doesn't work as (I) expected
% input=externalfilterbasefile.html, % this doesn't exist, but may be should?
output=externalfilterbasefile-full.png, % because of how webkit2png works
filtercommand=htmlfiltercommand,
readcommand=htmlexamplereadcommand,
cache=yes,
indentnext=no]
defhtmlexamplereadcommand#1%
{bpartypehtmlfile{externalfilterinputfile}epar
bparexternalfigure[#1][frame=on,maxwidth=.5textwidth]epar}
deffirefoxpath{/Applications/Firefox.app/Contents/MacOS}
defchromepath{/Applications/Google Chrome.app/Contents/MacOS}
defhtmlfiltercommand
{htmlconcatenatepartshtmlobtainpng}
defhtmlconcatenateparts
{cat "html-before-html-example.tmp" "externalfilterinputfile" "html-after-html-example.tmp" > "tmp.html";}
defhtmlobtainpng
{webkit2png
%--dir="auxfiles" % doesn't work correctly with externalfilter
--fullsize
--width=1 % to get the minimum
--height=1 % to get the minimum
--scale=1 % unused
--filename="externalfilterbasefile"
"tmp.html";}
% {firefoxpath/firefox
% -screenshot
% "externalfilteroutputfile"
% "externalfilterinputfile"}
% {chromepath/Google Chrome
% --headless
% --screenshot="externalfilteroutputfile"
% "externalfilterinputfile"}
starttext
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<pre>
1 2 3 4 5 6 7 <b>Forever,</b><br/>
8 9 10 11 12 13 14 and ever.<br/>
15 16 17 18 19 20 21
</pre>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure inlinehtml{<b>alo</b>} dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
stopchapter
stoptext
Fordirectory=outputto work, you need to create the directory on your own.
– Aditya
Nov 18 at 7:07
For adding before and after content, search forbufferbeforeandbufferafterin the documentation of the filter module
– Aditya
Nov 18 at 7:13
I see, still it doesn't work entirely well (or as expected) withwebkit2pngso I decided not to use it. Andbufferbeforeandbufferafteris of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.
– Manuel
Nov 18 at 13:07
Doeswebkit2png --filename=html/test.tmp html/test.pngwork from the command line? If so, then it should also work from within the filter module. Regardingbufferbeforretc, I don't understand what you mean by print those things while processing a different one. Can you give an example?
– Aditya
Nov 18 at 14:28
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I writestarthtmlexample <b>bold</bold> stophtmlexampleI want the file to be processed to have<html><body><b>bold</b></body></html>for example.
– Manuel
Nov 18 at 14:56
|
show 3 more comments
up vote
3
down vote
up vote
3
down vote
This is an answer as a work-in-progress of what I'm trying to achieve, with the suggestion of Henri Menke of using filter module, in company with webkit2png.
At the moment this “just works”. But it's far from the definitive. I'll leave it here until (hopefully) someone writes a better and more complete answer.
Things that I hope I can change:
- save every extra file (from
filter,vim, andwebkit2png) in anfolder/so that the current directory is not polluted;
This one is solved through some hacks in the bash commands.
have some “pre” and “post” code so one does not need to write everything (for example now I need to write<html><body>etc.), ideally the input would be for example
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
manage correctly the sizes of the image, and the fonts, and everything. Right now it's very fragile (I've done by hand the adjust of
font-size: 30in the webpage).
add some options to configure each example individually:
- for example if we want examples side by side (for small examples), something like
starthtmlexample[sidebyside]orstarthtmlexample[sidebyside=30]or something like that to say what part is for the code and which part for the image, and also another option to show first the code then below the result; - options to change the pre-code, and post-code for an individual piece;
some way of altering what's shown and what's written, for example
starthmlexample
<p> Lorem ipsum ... </p>
realhtml
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
stophtmlexample
- for example if we want examples side by side (for small examples), something like
an inline way of doing this that ends up working in kind of tables:
startxtable
startxrow
inlinehtmlexample{<b>bold</b>}
stopxrow
sartxrow
inlinehtmlexample{<i>cursive</i>}
stopxrow
stopxtable
to align many small results one below another.
right now I can't remember more things I thought (the
frame=onis jut to know what's happening at the moment, that I don't actually know many things I'm using here); and of courseget a decent base design so that it looks better than the default I wrote.
Here's the code:
usemodule[vim]
definevimtyping
[html]
[syntax=html,
directory=auxfiles,
before={blanksetupbodyfont[small]},
after={blank},
lines=split]
startbuffer [before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
savebuffer [before-html-example] [before-html-example]
% startbuffer [left-html-example]
%
% stopbuffer
%
% startbuffer [right-html-example]
%
% stopbuffer
startbuffer [after-html-example]
</body>
</html>
stopbuffer
savebuffer [after-html-example] [after-html-example]
usemodule[filter]
defineexternalfilter
[htmlexample]
[%directory=auxfiles, % this doesn't work as (I) expected
% input=externalfilterbasefile.html, % this doesn't exist, but may be should?
output=externalfilterbasefile-full.png, % because of how webkit2png works
filtercommand=htmlfiltercommand,
readcommand=htmlexamplereadcommand,
cache=yes,
indentnext=no]
defhtmlexamplereadcommand#1%
{bpartypehtmlfile{externalfilterinputfile}epar
bparexternalfigure[#1][frame=on,maxwidth=.5textwidth]epar}
deffirefoxpath{/Applications/Firefox.app/Contents/MacOS}
defchromepath{/Applications/Google Chrome.app/Contents/MacOS}
defhtmlfiltercommand
{htmlconcatenatepartshtmlobtainpng}
defhtmlconcatenateparts
{cat "html-before-html-example.tmp" "externalfilterinputfile" "html-after-html-example.tmp" > "tmp.html";}
defhtmlobtainpng
{webkit2png
%--dir="auxfiles" % doesn't work correctly with externalfilter
--fullsize
--width=1 % to get the minimum
--height=1 % to get the minimum
--scale=1 % unused
--filename="externalfilterbasefile"
"tmp.html";}
% {firefoxpath/firefox
% -screenshot
% "externalfilteroutputfile"
% "externalfilterinputfile"}
% {chromepath/Google Chrome
% --headless
% --screenshot="externalfilteroutputfile"
% "externalfilterinputfile"}
starttext
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<pre>
1 2 3 4 5 6 7 <b>Forever,</b><br/>
8 9 10 11 12 13 14 and ever.<br/>
15 16 17 18 19 20 21
</pre>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure inlinehtml{<b>alo</b>} dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
stopchapter
stoptext
This is an answer as a work-in-progress of what I'm trying to achieve, with the suggestion of Henri Menke of using filter module, in company with webkit2png.
At the moment this “just works”. But it's far from the definitive. I'll leave it here until (hopefully) someone writes a better and more complete answer.
Things that I hope I can change:
- save every extra file (from
filter,vim, andwebkit2png) in anfolder/so that the current directory is not polluted;
This one is solved through some hacks in the bash commands.
have some “pre” and “post” code so one does not need to write everything (for example now I need to write<html><body>etc.), ideally the input would be for example
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
manage correctly the sizes of the image, and the fonts, and everything. Right now it's very fragile (I've done by hand the adjust of
font-size: 30in the webpage).
add some options to configure each example individually:
- for example if we want examples side by side (for small examples), something like
starthtmlexample[sidebyside]orstarthtmlexample[sidebyside=30]or something like that to say what part is for the code and which part for the image, and also another option to show first the code then below the result; - options to change the pre-code, and post-code for an individual piece;
some way of altering what's shown and what's written, for example
starthmlexample
<p> Lorem ipsum ... </p>
realhtml
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
stophtmlexample
- for example if we want examples side by side (for small examples), something like
an inline way of doing this that ends up working in kind of tables:
startxtable
startxrow
inlinehtmlexample{<b>bold</b>}
stopxrow
sartxrow
inlinehtmlexample{<i>cursive</i>}
stopxrow
stopxtable
to align many small results one below another.
right now I can't remember more things I thought (the
frame=onis jut to know what's happening at the moment, that I don't actually know many things I'm using here); and of courseget a decent base design so that it looks better than the default I wrote.
Here's the code:
usemodule[vim]
definevimtyping
[html]
[syntax=html,
directory=auxfiles,
before={blanksetupbodyfont[small]},
after={blank},
lines=split]
startbuffer [before-html-example]
<html>
<head>
<meta charset="utf-8">
</head>
<body style="line-height: 1.5; font-size: 30">
stopbuffer
savebuffer [before-html-example] [before-html-example]
% startbuffer [left-html-example]
%
% stopbuffer
%
% startbuffer [right-html-example]
%
% stopbuffer
startbuffer [after-html-example]
</body>
</html>
stopbuffer
savebuffer [after-html-example] [after-html-example]
usemodule[filter]
defineexternalfilter
[htmlexample]
[%directory=auxfiles, % this doesn't work as (I) expected
% input=externalfilterbasefile.html, % this doesn't exist, but may be should?
output=externalfilterbasefile-full.png, % because of how webkit2png works
filtercommand=htmlfiltercommand,
readcommand=htmlexamplereadcommand,
cache=yes,
indentnext=no]
defhtmlexamplereadcommand#1%
{bpartypehtmlfile{externalfilterinputfile}epar
bparexternalfigure[#1][frame=on,maxwidth=.5textwidth]epar}
deffirefoxpath{/Applications/Firefox.app/Contents/MacOS}
defchromepath{/Applications/Google Chrome.app/Contents/MacOS}
defhtmlfiltercommand
{htmlconcatenatepartshtmlobtainpng}
defhtmlconcatenateparts
{cat "html-before-html-example.tmp" "externalfilterinputfile" "html-after-html-example.tmp" > "tmp.html";}
defhtmlobtainpng
{webkit2png
%--dir="auxfiles" % doesn't work correctly with externalfilter
--fullsize
--width=1 % to get the minimum
--height=1 % to get the minimum
--scale=1 % unused
--filename="externalfilterbasefile"
"tmp.html";}
% {firefoxpath/firefox
% -screenshot
% "externalfilteroutputfile"
% "externalfilterinputfile"}
% {chromepath/Google Chrome
% --headless
% --screenshot="externalfilteroutputfile"
% "externalfilterinputfile"}
starttext
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<strong>Hello</strong>, <em>world</em>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
starthtmlexample
<pre>
1 2 3 4 5 6 7 <b>Forever,</b><br/>
8 9 10 11 12 13 14 and ever.<br/>
15 16 17 18 19 20 21
</pre>
stophtmlexample
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure inlinehtml{<b>alo</b>} dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
stopchapter
stoptext
edited Nov 18 at 14:54
answered Nov 17 at 14:36
Manuel
21k845105
21k845105
Fordirectory=outputto work, you need to create the directory on your own.
– Aditya
Nov 18 at 7:07
For adding before and after content, search forbufferbeforeandbufferafterin the documentation of the filter module
– Aditya
Nov 18 at 7:13
I see, still it doesn't work entirely well (or as expected) withwebkit2pngso I decided not to use it. Andbufferbeforeandbufferafteris of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.
– Manuel
Nov 18 at 13:07
Doeswebkit2png --filename=html/test.tmp html/test.pngwork from the command line? If so, then it should also work from within the filter module. Regardingbufferbeforretc, I don't understand what you mean by print those things while processing a different one. Can you give an example?
– Aditya
Nov 18 at 14:28
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I writestarthtmlexample <b>bold</bold> stophtmlexampleI want the file to be processed to have<html><body><b>bold</b></body></html>for example.
– Manuel
Nov 18 at 14:56
|
show 3 more comments
Fordirectory=outputto work, you need to create the directory on your own.
– Aditya
Nov 18 at 7:07
For adding before and after content, search forbufferbeforeandbufferafterin the documentation of the filter module
– Aditya
Nov 18 at 7:13
I see, still it doesn't work entirely well (or as expected) withwebkit2pngso I decided not to use it. Andbufferbeforeandbufferafteris of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.
– Manuel
Nov 18 at 13:07
Doeswebkit2png --filename=html/test.tmp html/test.pngwork from the command line? If so, then it should also work from within the filter module. Regardingbufferbeforretc, I don't understand what you mean by print those things while processing a different one. Can you give an example?
– Aditya
Nov 18 at 14:28
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I writestarthtmlexample <b>bold</bold> stophtmlexampleI want the file to be processed to have<html><body><b>bold</b></body></html>for example.
– Manuel
Nov 18 at 14:56
For
directory=output to work, you need to create the directory on your own.– Aditya
Nov 18 at 7:07
For
directory=output to work, you need to create the directory on your own.– Aditya
Nov 18 at 7:07
For adding before and after content, search for
bufferbefore and bufferafter in the documentation of the filter module– Aditya
Nov 18 at 7:13
For adding before and after content, search for
bufferbefore and bufferafter in the documentation of the filter module– Aditya
Nov 18 at 7:13
I see, still it doesn't work entirely well (or as expected) with
webkit2png so I decided not to use it. And bufferbefore and bufferafter is of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.– Manuel
Nov 18 at 13:07
I see, still it doesn't work entirely well (or as expected) with
webkit2png so I decided not to use it. And bufferbefore and bufferafter is of no use, because I want to input certain things, and then print those things while processing a different one. For now I think I could rely on bash commands, but I wrote this question in case someone showed a nice solution that is entirely ConTeXt / Lua, rather than hacks here and there.– Manuel
Nov 18 at 13:07
Does
webkit2png --filename=html/test.tmp html/test.png work from the command line? If so, then it should also work from within the filter module. Regarding bufferbeforr etc, I don't understand what you mean by print those things while processing a different one. Can you give an example?– Aditya
Nov 18 at 14:28
Does
webkit2png --filename=html/test.tmp html/test.png work from the command line? If so, then it should also work from within the filter module. Regarding bufferbeforr etc, I don't understand what you mean by print those things while processing a different one. Can you give an example?– Aditya
Nov 18 at 14:28
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I write
starthtmlexample <b>bold</bold> stophtmlexample I want the file to be processed to have <html><body><b>bold</b></body></html> for example.– Manuel
Nov 18 at 14:56
I updated the answer. Well, the idea is to write in the example only a part of the whole file that is processed. So if I write
starthtmlexample <b>bold</bold> stophtmlexample I want the file to be processed to have <html><body><b>bold</b></body></html> for example.– Manuel
Nov 18 at 14:56
|
show 3 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f460367%2fcontext-html-source-code-side-by-side-to-the-result-from-the-web%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Looks like a job for
filter.– Henri Menke
Nov 16 at 20:38
Thanks, yes, it looks like it :) Not able to make it work, but working on it. Nice to see this things exist.
– Manuel
Nov 16 at 21:44