背景

背景

背景

希望将动态派生的文本绘制为跨越多页的段落的“图例”。

版本:2023.07.18 22:07

问题

文本没有出现在所需的位置。

代码

这是一个独立的例子:

\startbuffer[demo]
<html>
<body>
<div class="concurrent" data-title="Berth 5" data-location="San Diego">
Text Goes Here
</div>
<div class="concurrent" data-title="Road" data-location="Beale AFB">
Different Text Goes Here
</div>
</body>
</html>
\stopbuffer

\startxmlsetups xml:xhtml
  \xmlsetsetup{\xmldocument}{*}{-}
  \xmlsetsetup{\xmldocument}{html|body}{xml:*}
  \xmlsetsetup{\xmldocument}{div}{xml:*}
\stopxmlsetups

\xmlregistersetup{xml:xhtml}

\startxmlsetups xml:html
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:body
  \xmlflush{#1}
\stopxmlsetups

\startxmlsetups xml:div
  \setvariable{div}{\xmlatt{#1}{class}}{#1}
  \start[\xmlatt{#1}{class}]\xmlflush{#1}\stop
\stopxmlsetups

\startusableMPgraphic{GraphicConcurrent}
  begingroup;
    string legend;

    picture title;
    picture border;
    picture bg;

    numeric tw;
    numeric th;

    legend := \MPstring{concurrent};

    title := nullpicture;
    border := nullpicture;
    bg := textext( "\strut " & legend );

    tw := xpart lrcorner bg - xpart llcorner bg;
    th := ypart ulcorner bg - ypart llcorner bg;

    addto title also image(
      fill unitsquare
        xysized (tw + 8, th)
        shifted ulcorner multipars[1]
        shifted 28 right
        shifted 8 down
        withcolor white;
        
      draw
        textext.drt( legend )
        shifted ulcorner multipars[1]
        shifted 32 right
        shifted 3 down;
    );

    addto border also image(
      for i = 1 upto nofmultipars:
        draw
          llcorner multipars[i] --
          ulcorner multipars[i] shifted 8 down ..
          ulcorner multipars[i] shifted 8 right --
          urcorner multipars[i]
          withpen pencircle scaled 0.75 withcolor black;
      endfor; 
    );

    draw image(
      draw border;
      draw title;
    );
  endgroup;
\stopusableMPgraphic

\definetextbackground[TextConcurrentFrame][
  mp=GraphicConcurrent,
  frame=off,
  topoffset=1em,
  leftoffset=1em,
  location=paragraph,
]

\definestartstop[concurrent][
  before={%
    \blank[big]%
    LEGEND: \xmlatt{\getvariable{div}{concurrent}}{data-title}%
    \blank[big]%
    \setMPtext{concurrent}{\xmlatt{\getvariable{div}{concurrent}}{data-title}}
    \startTextConcurrentFrame},
  after={\stopTextConcurrentFrame\blank[big]},
]

\starttext
  \xmlprocessbuffer{main}{demo}{}
\stoptext

输出:

无输出

更改以下行:

    legend := \MPstring{concurrent};

到:

    legend := 'LEGENDARY';

结果是:

图例输出

问题似乎出在下面这一行:

    \setMPtext{concurrent}{\xmlatt{\getvariable{div}{concurrent}}{data-title}}

不被尊重:

    legend := \MPstring{concurrent};

尽管以下行显示两个不同div元素的值不同:

    LEGEND: \xmlatt{\getvariable{div}{concurrent}}{data-title}%

问题

当代码绘制文本时,如何使data-title="Berth 5"属性值显示为图例标题?GraphicConcurrent

有关的

答案1

使用\setxvariable\getvariable根据数字索引存储和检索变量值。有两个索引:一个用于存储文本值,一个用于检索文本值。请注意使用\definenumber以确保仅在遇到新的“并发”环境时才增加检索变量。

来自邮件列表:

\definenumber[ConcurrentTextSetCounter][prefix=no]
\definenumber[ConcurrentTextGetCounter][prefix=no]

\setnumber[ConcurrentTextSetCounter][0]
\setnumber[ConcurrentTextGetCounter][0]

% Map each label as global key/value pairs.
\def\ConcurrentTextSet#1{%
  \incrementnumber[ConcurrentTextSetCounter]%
  \setxvariable
    {concurrent}
    {text:\rawcountervalue[ConcurrentTextSetCounter]}
    {#1}}

% Account for the counter incrementing twice and the index being 1-based.
\def\ConcurrentTextGet{%
  \incrementnumber[ConcurrentTextGetCounter]%
  \getvariable
    {concurrent}
    {text:\number\numexpr1+\rawcountervalue[ConcurrentTextGetCounter]/2\relax}}

\startuseMPgraphic{GraphicConcurrent}
  numeric index;
  index := 1;

  % Differentiate between new text blocks and those crossing pages.
  if (multikind[ index ] = "single") or (multikind[ index ] = "first"):
    string legend;
    legend := "\ConcurrentTextGet";

    % For new text blocks, write the title.
    picture p;
    p := textext.rt( legend )
      shifted ulcorner multipars[ index ]
      shifted (1cm, 0);

    % Draw the horizontal rule only for the initial text block.
    draw
      ulcorner multipars[ index ] shifted (1mm + xpart lrcorner p, 0) --
      urcorner multipars[ index ];

    % Draw the vertical rule for the initial text block.
    draw
      llcorner multipars[ index ] --
      ulcorner multipars[ index ] --
      ulcorner multipars[ index ] shifted (9mm, 0);

    draw p;
  else:
    % Draw only the vertical rule only when crossing page boundaries.
    draw
      llcorner multipars[ index ] --
      ulcorner multipars[ index ];
  fi
\stopuseMPgraphic

\definetextbackground[TextConcurrentFrame][
  mp=GraphicConcurrent,
  frame=off,
  topoffset=1em,
  leftoffset=1em,
  before=\blank[2*big],
  after=\blank,
  location=paragraph,
]

\startsetups concurrent:before
  \ConcurrentTextSet{%
    % Be sure to format "a.m." and other special phrases.
    \expandafter\TextReplacement{%
      \xmlatt{\getvariable{div}{concurrent}}{data-title}%
    }
  }
  \startTextConcurrentFrame
\stopsetups

\startsetups concurrent:after
  \stopTextConcurrentFrame
\stopsetups

\definestartstop[concurrent][
  before=\directsetup{concurrent:before},
  after=\directsetup{concurrent:after}
]

相关内容