Difference between revisions of "Filecontents"

From ConTeXt wiki
Jump to navigationJump to search
(Implementation in ConTeXt)
 
(messages)
Line 8: Line 8:
 
<texcode>
 
<texcode>
 
\unprotect
 
\unprotect
 +
 +
\definemessageconstant {filecontents}
 +
 +
\startmessages all library: filecontents
 +
        title: filecontents
 +
            1: Overwriting file --
 +
            2: Writing file --
 +
\stopmessages
  
 
\def\startfilecontents
 
\def\startfilecontents
Line 15: Line 23:
  
 
\def\dostartfilecontents[#1]%
 
\def\dostartfilecontents[#1]%
   {\beforesplitstring#1\at.\to\filename
+
   {\doiffileexistselse{#1}
 +
    {\showmessage\m!filecontents{1}{#1}}
 +
    {\showmessage\m!filecontents{2}{#1}}%
 +
  \beforesplitstring#1\at.\to\filename
 
   \aftersplitstring #1\at.\to\extension
 
   \aftersplitstring #1\at.\to\extension
 
   \let\f!temporaryextension\extension
 
   \let\f!temporaryextension\extension

Revision as of 08:46, 17 April 2009

LaTeX provides a filecontent environment to embed text in your document which is written to a external document when your process it. The filename is given with the argument for the environment.

You could define a environment for the same effect with the buffer mechanism, the code for such a environment is:

\unprotect

\definemessageconstant {filecontents}

\startmessages all library: filecontents
        title: filecontents
            1: Overwriting file --
            2: Writing file --
\stopmessages

\def\startfilecontents
  {\begingroup
   \protectbuffersfalse
   \dostartfilecontents}

\def\dostartfilecontents[#1]%
  {\doiffileexistselse{#1}
     {\showmessage\m!filecontents{1}{#1}}
     {\showmessage\m!filecontents{2}{#1}}%
   \beforesplitstring#1\at.\to\filename
   \aftersplitstring #1\at.\to\extension
   \let\f!temporaryextension\extension
   \dostartbuffer[\filename][startfilecontents][stopfilecontents]}

\def\stopfilecontents
  {\doifmode{mkiv}{\savebuffer[\filename]\ctxlua{file.copy("\jobname-\filename.tmp","\filename.\f!temporaryextension")}}%
   \endgroup}

\protect

You could use this environment in your document in the following way.

\startfilecontents[song.txt]
If I could choose my paradise,
    And please myself with choice of bliss,
Then I would have your soft blue eyes
    And rosy little mouth to kiss!
Your lips, as smooth and tender, child,
As rose-leaves in a coppice wild.

If fate bade choose some sweet unrest,
    To weave my troubled life a snare,
Then I would say "her maiden breast
    And golden ripple of her hair;"
And weep amid those tresses, child,
Contented to be thus beguiled.

            (Thomas Asche - No and Yes)
\stopfilecontents