]> git.lyx.org Git - lyx.git/blob - lib/doc/ExternalMaterial.lyx
FILMagain changes (will need some work)
[lyx.git] / lib / doc / ExternalMaterial.lyx
1 #LyX 1.1 created this file. For more info see http://www.lyx.org/
2 \lyxformat 2.16
3 \textclass article
4 \language default
5 \inputencoding latin1
6 \fontscheme default
7 \graphics default
8 \paperfontsize default
9 \spacing single 
10 \papersize Default
11 \paperpackage a4
12 \use_geometry 0
13 \use_amsmath 0
14 \paperorientation portrait
15 \secnumdepth 3
16 \tocdepth 3
17 \paragraph_separation indent
18 \defskip medskip
19 \quotes_language english
20 \quotes_times 2
21 \papercolumns 1
22 \papersides 1
23 \paperpagestyle default
24
25 \layout Section
26
27 The external material inset
28 \layout Subsection
29
30 Background
31 \layout Standard
32
33 One often requested feature from LyX users is to be able to interface LyX
34  with XFig, Dia, or other similar applications that specialize in producing
35  a certain kind of diagram, figure, schematic or whatever material might
36  be relevant to include in your document.
37  Previously, it was only possible to include boring, static, fixed images
38  in LyX documents with the figure inset, but there are several limitations
39  attached to this approach:
40 \layout Itemize
41
42 If you want to change the figure, you have to invoke an external program
43  by hand
44 \layout Itemize
45
46 LyX does not notice that the referenced files change, so the on-screen display
47  can fast become obsolete, and this is aggravated by the lack of a means
48  of updating the display
49 \layout Itemize
50
51 The figure inset only supports PostScript material
52 \layout Itemize
53
54 The figure inset does not provide any mechanisms for coping with different
55  exported formats such as DocBook, HTML or rawAscii
56 \layout Standard
57
58 The external material inset attempts to solve all of these problems
59 \begin_float footnote 
60 \layout Standard
61
62 Even if the figure inset can't solve all problems, it is still valuable
63  because it does provide in-line preview of the figure, and supports advanced
64  geometric transformations with a comfortable user interface.
65 \end_float 
66 .
67  It does this by offering a general method to interface LyX to external
68  applications.
69  Instead of introducing a long list of different insets taylored for each
70  specific application, we chose to sacrify the in-line displaying of the
71  included material in order to provide a general construct to cover a wide
72  range of applications.
73  The result is the external inset.
74  An external inset presents itself in the document simply as a button, but
75  don't let this fool you.
76  When you click on it, a dialog will appear that allows you to chose exactly
77  what material to include, and in the following you will learn that this
78  is indeed a powerful mechanism that can solve all of the above problems,
79  and more.
80 \layout Subsection
81
82 How does it work?
83 \layout Standard
84
85 The external inset is based on the concept of a
86 \emph on 
87  template
88 \emph default 
89 .
90  A template is a specification of how LyX should interface with a certain
91  kind of material.
92  As bundled, LyX comes with predefined templates for XFig figures, Dia diagrams,
93  various raster format images, gnuplot, and more.
94  You can check the actual list by using the 
95 \family sans 
96 Insert\SpecialChar \menuseparator
97 Insert external material 
98 \family default 
99 command.
100  Furthermore, it is possible to roll your own template to support a specific
101  kind of material.
102  Later we'll describe in more detail what is involved, and hopefully you
103  will submit all the templates you create so we can include them in a later
104  LyX version.
105 \layout Standard
106
107 Another basic idea of the external inset is to distinguish between the original
108  file that serves as a base for final material and the produced file that
109  is included in your exported or printed document.
110  For example, consider the case of a figure produced with XFig.
111  The XFig application itself works on an original file with the 
112 \family typewriter 
113 .fig
114 \family default 
115  extension.
116  Within XFig, you create and change your figure, and when you are done,
117  you save the 
118 \family typewriter 
119 fig
120 \family default 
121 -file.
122  When you want to include the figure in your document, you invoke 
123 \family typewriter 
124 transfig
125 \family default 
126  in order to create a PostScript file that can readily be included in your
127  LaTeX file.
128  In this case, the 
129 \family typewriter 
130 .fig
131 \family default 
132  file is the original file, and the PostScript file is the produced file.
133 \layout Standard
134
135 This distinction is important in order to allow updating of the material
136  while you are in the process of writing the document.
137  Furthermore, it provides us with the flexibility that is needed to support
138  multiple export formats.
139  For instance, in the case of an Ascii resulting file, it is not exactly
140  an award-winning idea to include the figure as raw PostScript.
141  Instead, you'd either prefer to just include a reference to the figure,
142  or try to invoke some graphics to Ascii converter to make the final result
143  look similar to the real graphics.
144  The external material inset allows you to do this, because it is parameterized
145  on the different export formats that LyX supports.
146 \layout Standard
147
148 Besides supporting the production of different products according to the
149  exported format, the external inset supports tight integration with editing
150  and viewing applications.
151  In the case of an XFig figure, you are able to invoke xfig on the original
152  file with a single click from within the external inset in LyX, and also
153  preview the produced PostScript file with ghostview with another click.
154  No more fiddling around with the command line and/or file browsers to locate
155  and manipulate the original or produced files.
156  In this way, you are finally able to take full advantage of the many different
157  applications that are relevant to use when you write your documents, and
158  ultimately be more productive.
159 \layout Standard
160
161 So, all in all, LyX has information about a number of different programs
162  to use behind the scenes in order to realize all of this machinery.
163  This information, in fact, is exactly what is contained in the templates.
164  To each template, there is associated a list of command lines that are
165  used to invoke applications, convert the original file to the produced
166  file, and more.
167  This mechanism allows the advanced user to extend the capabilities of LyX
168  without fiddling with the source code.
169  It requires some footwork to define all the different commands and flags,
170  but luckily, the LyX team did all the hard work and specified these for
171  you.
172 \layout Standard
173
174 But before the trees grow into the skies, we have to admit that we did take
175  one tiny short-cut.
176  Since the external inset can produce many different kinds of produced files
177  to go with each exported format, one could also expect that it would be
178  possible to preview each product.
179  But, the LyX team decides against this in order to keep the user interface
180  simple.
181  Instead of providing a button for each exported file format, we decided
182  to introduce the concept of the primary file format and just have one button.
183  When you press 
184 \family sans 
185 View result
186 \family default 
187  in the external inset dialog, you will get a view of the produced file
188  in the primary file format.
189  And the primary file format is specified by your document class.
190  For most document classes, the primary file format is LaTeX, but for the
191  DocBook document classes, the primary file format is DocBook.
192  So, when you view the produced file, keep in mind that it will only be
193  a preview of what the main result will be.
194  If you want to see how other exported formats turn out, you have to export
195  them and preview them by hand.
196 \layout Subsection
197
198 The external material inset dialog
199 \layout Standard
200
201 You insert an external inset from the 
202 \family sans 
203 Insert
204 \family default 
205  menu.
206  When you do this, a button is inserted into your document, and the external
207  material inset dialog is shown.
208  This dialog allows you to describe exactly what material should be included,
209  and also how it should be included.
210  Furthermore, it provides access to the external applications to either
211  view, edit or produce the material that is used in the resulting file.
212 \layout Standard
213
214 At the top of this dialog, there is a drop-down list where you can chose
215  which template the inset should use.
216  Just below the template drop-down, there's an text area with a short blurp
217  about the chosen template that should help you use it.
218  Most often, it will provide a short description of the template, and a
219  few hints on how to parameterize the use of it.
220  Further down, you'll find a filename input field along with a browse-button
221  that allows you to chose which file should be included, with the standard
222  file browser.
223  Thus this field specifies the original file.
224  Since the produced file is automatically generated when needed, there is
225  no need to give access to it in the user interface.
226 \layout Standard
227
228 At the bottom of the dialog, you'll find a general input box called 
229 \family sans 
230 Parameters
231 \family default 
232 .
233  This box is generally used to parameterize the specific template.
234  The specific use should be covered in the help blurp associated with the
235  template, but in general it typically allows you to define variations on
236  how the produced file should be generated.
237 \layout Standard
238
239 At the right side of the dialog, you'll find three buttons: 
240 \family sans 
241 Edit file
242 \family default 
243
244 \family sans 
245 View result
246 \family default 
247 , and 
248 \family sans 
249 Update result
250 \family default 
251 .
252  These in turn allows you to edit your original file with the appropriate
253  editing application, view the produced file as included in the primary
254  format document, and finally force an update of the resulting material
255  in the primary format.
256  Normally, the 
257 \family sans 
258 Update result
259 \family default 
260  button will be disabled, because most templates are configured to automatically
261  update the produced file when needed.
262  In those cases, there is no need to force the production of a new produced
263  file.
264  However, some templates are configured to not be automatically producing
265  the residual product, because the cost of producing the produced file might
266  be so large that it would be a pain to do it all the time.
267  Those insets are known as 
268 \emph on 
269 manual
270 \emph default 
271  external insets.
272  In those cases, you can use the button to force the production of the produced
273  file exactly when you need it, and thus control the amount of work that
274  is done.
275  In fact, it is 
276 \emph on 
277 your
278 \emph default 
279  responsibility to do this to keep the produced files current at all times:
280  before printing, before exporting, before viewing, etc.
281  To help you with this task, you can use the 
282 \family sans 
283 Edit\SpecialChar \menuseparator
284 Update all external inset
285 \family default 
286  command that will update all external insets that use a manual template.
287  But be prepared that it might take some time for the updating to finish.
288 \layout Standard
289
290 At the bottom of the dialog, you find the normal 
291 \family sans 
292 OK
293 \family default 
294  and 
295 \family sans 
296 Cancel
297 \family default 
298  buttons.
299  The only thing worth mentioning about these is that any changes in the
300  template, filename or parameters are actually applied whenever you press
301  
302 \family sans 
303 Edit file
304 \family default 
305
306 \family sans 
307 View result
308 \family default 
309  or 
310 \family sans 
311 Update result
312 \family default 
313  buttons.
314  This implies that after using one of those, you will only be able to undo
315  changes that occured after the use of those buttons, by pressing 
316 \family sans 
317 Cancel
318 \family default 
319 .
320  Fortunately, you can use the general undo feature in LyX to revert to a
321  previous state.
322 \layout Subsection
323
324 Examples
325 \layout Standard
326
327 In this section, we should include some examples of use of the external
328  material inset.
329  Those examples could include:
330 \layout Itemize
331
332 External raster images
333 \layout Itemize
334
335 External XFig figures
336 \layout Itemize
337
338 Chess diagrams
339 \layout Itemize
340
341 Sound samples
342 \layout Itemize
343
344 The use of makefiles
345 \layout Itemize
346
347 Recursive external LyX templates
348 \layout Subsection
349
350 The external template configuration file
351 \layout Standard
352
353 It is relatively easy to add custom external template definitions to LyX.
354  However, be aware this doing this in an careless manner most probably 
355 \emph on 
356 will
357 \emph default 
358  introduce an easily exploitable security hole.
359  So before you do this, please read the discussion about security which
360  will follow later.
361 \layout Standard
362
363 Having said that, we encourage you to submit any interesting templates that
364  you create.
365  
366 \layout Standard
367
368 The external templates are defined in the 
369 \family typewriter 
370 lib/external_templates
371 \family default 
372  file.
373  You can place your own version in 
374 \family typewriter 
375 .lyx/external_templates
376 \family default 
377 .
378  At some point in time, hopefully somebody will document the template contents,
379  and the syntax used to define your templates.
380 \layout Subsection
381
382 The substitution mechanism
383 \layout Standard
384
385 When the external material inset invokes an external program, it is done
386  on the basis of a command defined in the template configuration file.
387  These commands can contain various macros that are expanded before execution.
388  Execution always take place in the directory of the containing document.
389 \layout Standard
390
391 Also, whenever an external inset is to be displayed, the name will be produced
392  by the substitution mechanism.
393 \layout Standard
394
395 The available macros are the following:
396 \layout Description
397
398 $$FName The filename of the file specified in the external inset dialog.
399 \layout Description
400
401 $$Basename The filename without the extension.
402 \layout Description
403
404 $$Tempname A name and full path to a temporary file which will be automatically
405  deleted whenever the containing document is closed, or the external inset
406  deleted.
407 \layout Description
408
409 $$Contents(
410 \begin_inset Quotes eld
411 \end_inset 
412
413 filename.ext
414 \begin_inset Quotes erd
415 \end_inset 
416
417 ) This macro will expand to the contents of the file with the name 
418 \family typewriter 
419 filename.ext
420 \family default 
421 .
422 \layout Description
423
424 $$Sysdir This macro will expand to the absolute path of the system directory.
425  This is typically used to point to the various helper scripts that are
426  bundled with LyX.
427 \layout Standard
428
429 In addition to these, the facility will expand general environment variables
430  with a syntax like 
431 \family typewriter 
432 ${PATH}
433 \family default 
434 .
435 \layout Subsection
436
437 Security discussion
438 \layout Standard
439
440 The external material inset interfaces with a lot of external programs and
441  does so automatically, so we have to consider the security implications
442  of this.
443  In particular, since you have the option of including your own filenames
444  and/or parameter strings and those are expanded into a command, it seems
445  that it would be possible to create a malicious document which executes
446  arbitrary commands when a user views or prints the document.
447  This is something we definately want to avoid.
448 \layout Standard
449
450 However, since the external program commands are specified in the template
451  configuration file only, there are no security issues if LyX is properly
452  configured with safe templates only.
453  This is so because the external programs are invoked with the 
454 \family typewriter 
455 execvp
456 \family default 
457 -system call rather than the 
458 \family typewriter 
459 system
460 \family default 
461  system-call, so it's not possible to execute arbitrary commands from the
462  filename or parameter section via the shell.
463 \layout Standard
464
465 This also implies that you are restricted in what command strings you can
466  use in the external material templates.
467  In particular, pipes and redirection are not readily available.
468  This has to be so if LyX should remain safe.
469  If you want to use some of the shell features, you should write a safe
470  script to do this in a controlled manner, and then invoke the script from
471  the command string.
472  In the 
473 \family typewriter 
474 lib/scripts
475 \family default 
476  directory of the LyX installation, you can find a safe wrapper script 
477 \family typewriter 
478 general_command_wrapper.py
479 \family default 
480  that supports redirection of input and output.
481  That can serve as an example for how to write safe template scripts.
482  For a more advanced example that uses 
483 \family typewriter 
484 fork
485 \family default 
486  and friends, take a look at the 
487 \family typewriter 
488 pic2ascii.py
489 \family default 
490  converter script.
491 \layout Standard
492
493 It is possible to design a template that interacts directly with the shell,
494  but since this would allow a malicious user to execute arbitrary commands
495  by writing clever filenames and/or parameters, we generally recommend that
496  you only use safe scripts that work with the 
497 \family typewriter 
498 execvp
499 \family default 
500  system call in a controlled manner.
501  Of course, for use in a controlled environment, it can be tempting to just
502  fall back to use ordinary shell scripts.
503  If you do so, be aware that you 
504 \emph on 
505 will
506 \emph default 
507  provide an easily exploitable security hole in your system.
508  Of course it stands to reason that such unsafe templates will never be
509  included in the standard LyX distribution, although we do encourage people
510  to submit new templates in the open source tradition.
511  But LyX as shipped from the official distribution channels will never have
512  unsafe templates.
513 \layout Standard
514
515 The external material inset provides a lot of power, and you have to be
516  careful not to introduce security hazards with this power.
517  A subtile error in a single line in an innocent looking script can open
518  the door to huge security problems.
519  So if you do not fully understand the issues, we recommend that you consult
520  a knowledgable security professional or the LyX development team if you
521  have any questions about whether a given template is safe or not.
522  And do this before you use it in an uncontrolled environment.
523 \layout Subsection
524
525 The future of the external inset
526 \layout Standard
527
528 The current implementation of the external inset is a stable and powerful
529  construct that provides raw access to the inner parts of LyX, but as with
530  any feature in LyX, it should always be considered work-in-progress.
531  When and if somebody has time to continue work on it, here are some general
532  directions that could be approached:
533 \layout Itemize
534
535 Support in-line previewing in various formats, rather than the button text
536  we are restricted to now.
537 \layout Itemize
538
539 Support in-line editing using OpenParts or any other relevant protocol.
540 \layout Itemize
541
542 Extend the dynamic information to have optional parameter fields for the
543  conversion commands in all export formats, and to have optional parameter
544  fields for what is produced into all the different exported formats.
545  At the moment, we are restricted to only one parameter string that is multiplex
546 ed across these many applications.
547  Also, a change like this would allow us to get rid of the strange primary
548  target format restrictions.
549 \layout Itemize
550
551 Extend the framework to provide more intelligent customization options in
552  addition to the rather simplistic raw parameter strings.
553  With a suitable scripting language, it would be possible to implement user
554  friendly versions of many customizable insets that supports a wide range
555  of formats, LaTeX packages, editors, etc.
556 \the_end