ConTeXt、HTML 源代码与来自网络的结果并排显示

ConTeXt、HTML 源代码与来自网络的结果并排显示

我想编写一个包含一些 HTML 代码的文档,并在其旁边显示网页结果。

基本上就像

\starthtmlexample
  <strong>Hello</strong>, <em>world<em>
\stophtmlexample

将会把突出显示的代码与其在网站上的显示结果并排打印。

我想到的是环境

  • 为每个示例创建一个以 .html 为扩展名的新文件,其中存储一些位于环境内容之前的代码;
  • 然后是环境的内容,在这种情况下<strong>Hello</strong>, <em>world<em>
  • 一些用于关闭文件的帖子 HTML 标签;
  • 然后使用命令处理文件

    firefox -screenshot "screenshot-\n.png" "file:///Users/blah/blah/file-\n.html"
    

    (好吧,使用选项--window-size="\certainwidth,\certainheight")获取 .png

  • 然后并排添加代码(用 着色\usemodule[vim])和结果图像。

这是一个“模型”

\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

需要说明的是,我对 ConTeXt 还比较陌生,所以这个问题可能可以通过其他系统解决,这是一个 XY 问题。

我的想法是编写一个包含许多 html 代码小示例的文档。

答案1

我无法安装 webkit2png,因此这里举例说明如何使用pandoc转换htmlpdf。首先,让我们从显示源的基本功能开始。请注意,您需要本地目录调用html才能使其工作。

\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 \externalfilterinputfile\space -o \externalfilteroutputfile},
      purge=no,
      cache=yes,
      readcommand=\DisplaySourceAndOutput,
    ]

\def\DisplaySourceAndOutput#1%
    {\typefile{\externalfilterinputfile}
     \externalfigure[#1][frame=on,width=10cm]}


\starttext
Hello
\starthtmlexample
  <strong>Hello</strong>, <em>world<em>
\stophtmlexample
\stoptext

在此处输入图片描述

现在,让我们添加语法高亮,并从显示的代码片段中删除前后代码。因此,我们将 的定义更改为\DisplaySourceAndOutput如下:

\usemodule[vim]

\definevimtyping[html][syntax=html,directory=html]

\def\DisplaySourceAndOutput#1%
    {\typehtmlfile[start=6,stop=-2]{\externalfilterinputfile}
     \externalfigure[#1][frame=on,width=10cm]}

该值start=6要求typehtmlfile显示从该行开始的输出6th(因为before-html-example有 5 行)。该值stop=-2要求不显示最后 2 行(因为after-html-example有 2 行)。

上面的例子现在给出:

在此处输入图片描述

现在您需要做的就是\DisplaySourceAndOutput按照您希望显示的方式进行修改。

编辑

我不明白你的评论是什么意思:

例如允许\starthtmlexample ... \realhtmlexample ... \stophtmlexample或诸如此类的事情。

假设您想忽略部分环境,这实际上相对容易(如果您使用缓冲区)。例如,以下是删除!!START!!缓冲区中字符串之前的所有内容的代码:

\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

答案2

这是我正在尝试实现的一个正在进行的工作的答案,Henri Menke 建议使用filter模块,与webkit2png

目前,这种方法“有效”。但还远未达到最终效果。我将保留此答案,直到(希望)有人写出更好、更完整的答案。

我希望能够改变的事情:

  • 将每个额外的文件(来自filtervimwebkit2png)保存在其中,folder/以免当前目录受到污染;
  • 这个问题可以通过对 bash 命令进行一些修改来解决。

    有一些“前”和“后”代码,这样就不需要写所有内容(例如现在我需要写<html><body>等等),理想情况下的输入将是例如

    \starthtmlexample
      <strong>Hello</strong>, <em>world</em>
    \stophtmlexample
    
  • font-size: 30正确管理图像的大小、字体和所有内容。现在它非常脆弱(我已经在网页上手动调整了)。

  • 添加一些选项来单独配置每个示例:

    • 例如,如果我们想要并排显示示例(对于小示例),类似\starthtmlexample[sidebyside]\starthtmlexample[sidebyside=30]类似的内容来说明哪部分用于代码,哪部分用于图像,还有另一个选项是先显示代码,然后显示结果;
    • 用于更改单个片段的前置编码和后置编码的选项;
    • 例如,以某种方式改变所展示和所写的内容

      \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
      
  • 一种内联方式最终以表格形式工作:

    \startxtable
      \startxrow
        \inlinehtmlexample{<b>bold</b>}
      \stopxrow
      \sartxrow
        \inlinehtmlexample{<i>cursive</i>}
      \stopxrow
    \stopxtable
    

    将许多小结果一个接一个地对齐。

  • 现在我记不起我想到的更多东西(只是frame=on知道此刻发生了什么,我实际上并不知道我在这里使用的很多东西);当然

  • 获得一个不错的基础设计,以便它看起来比我写的默认设计更好。

代码如下:

\usemodule[vim]

\definevimtyping
  [html]
  [syntax=html,
   directory=auxfiles,
   before={\blank\setupbodyfont[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]

\def\htmlexamplereadcommand#1%
  {\bpar\typehtmlfile{\externalfilterinputfile}\epar
   \bpar\externalfigure[#1][frame=on,maxwidth=.5\textwidth]\epar}

\def\firefoxpath{/Applications/Firefox.app/Contents/MacOS}
\def\chromepath{/Applications/Google\ Chrome.app/Contents/MacOS}

\def\htmlfiltercommand
  {\htmlconcatenateparts\htmlobtainpng}
\def\htmlconcatenateparts
  {cat "html-before-html-example.tmp" "\externalfilterinputfile" "html-after-html-example.tmp" > "tmp.html";}
\def\htmlobtainpng
  {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

相关内容