Wei-Wei Guo napisaƂ(a):

Hello,

> I'm not trying to add files. I want to implement things like this:
> 
>    source
>      |-- index.rst
>      |-- mydict
>            |-- index.rst
>            |-- mytest.rst
> 
> building to get:
> 
>    build
>      |-- index.tex
>      |-- mydict
>            |-- index.tex
>            |-- mytest.tex
> 
> The LaTeX builder will put all the building results into one .tex file. I 
> don't know
> how to implement it, so I ask for some clues.

To stick with latex_documents I would say:
  - create your own builder which is a subclass of LaTeXBuilder
  - write your own init_document_data method, which will create
    the directories that you need in your build directory
  - add the files you would like to produce to latex_document variable
  - enjoy ;-)

An example:

---------- mylatexbuilder.py ----------------
import os
from sphinx.builders.latex import LaTeXBuilder
from sphinx.util import SEP

def _mkdir(newdir):
     """works the way a good mkdir should :)
         - already exists, silently complete
         - regular file in the way, raise an exception
         - parent directory(ies) does not exist, make them as well
        source: http://code.activestate.com/recipes/82465/
     """

     if os.path.isdir(newdir):
         pass
     elif os.path.isfile(newdir):
         raise OSError("a file with the same name as the desired " \
                       "dir, '%s', already exists." % newdir)
     else:
         head, tail = os.path.split(newdir)
         if head and not os.path.isdir(head):
             _mkdir(head)
         #print "_mkdir %s" % repr(newdir)
         if tail:
             os.mkdir(newdir)

class MyLaTeXBuilder(LaTeXBuilder):
    name = 'mylatex'

    def init_document_data(self):
       preliminary_document_data = map(list, self.config.latex_documents)
       if not preliminary_document_data:
          self.warn('no "latex_documents" config value found; no documents '
                    'will be written')
          return
       # assign subdirs to titles
       self.titles = []
       for entry in preliminary_document_data:
          docname = entry[0]
          if docname not in self.env.all_docs:
             self.warn('"latex_documents" config value references unknown '
                       'document %s' % docname)
             continue

          # below is the only line that I added the rest comes from
          # sphinx/builders/latex.py
          _mkdir(os.path.join(self.outdir, os.path.dirname(entry[1])))

          self.document_data.append(entry)
          if docname.endswith(SEP+'index'):
             docname = docname[:-5]
          self.titles.append((docname, entry[2]))

def setup(app):
    app.add_builder(MyLaTeXBuilder)
--------------------------------------

Now add it as an extension in your conf.py and change the
latex_documents variable. Let's say that it looks like this:

latex_documents = [
   ('index', 'q.tex', u'q Documentation',
    u'q', 'manual'),
   ('mytest/mytest', 'mytest/mytest.tex', u'mytest', u'mytest', 'manual'),
]

and now, just run it:

$ sphinx-build -b mylatex -d _build/doctrees   . _build/mylatex

and the result is:

$ ls _build/mylatex/q.tex
_build/mylatex/q.tex
$ ls _build/mylatex/mytest/mytest.tex
_build/mylatex/mytest/mytest.tex


HTH,
-- 
Regards,
Wojtek Walczak,
http://tosh.pl/gminick/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sphinx-dev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sphinx-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to