1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4# Copyright (C) 2016-2018 g10 Code GmbH
5# Copyright (C) 2015 Ben McGinnes <ben@adversary.org>
6#
7# This library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU Lesser General Public
9# License as published by the Free Software Foundation; either
10# version 2.1 of the License, or (at your option) any later version.
11#
12# This library is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15# Lesser General Public License for more details.
16#
17# You should have received a copy of the GNU Lesser General Public
18# License along with this library; if not, write to the Free Software
19# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
20
21from __future__ import absolute_import, print_function, unicode_literals
22
23import glob
24import os
25import os.path
26import shutil
27import subprocess
28import sys
29import sysconfig
30
31from shutil import which
32
33del absolute_import, print_function, unicode_literals
34
35try:
36    emacs = os.path.realpath(which("emacs"))
37except TypeError as e:
38    emacs = None
39
40try:
41    makeinfo = os.path.realpath(which("makeinfo"))
42except TypeError as e:
43    makeinfo = None
44
45try:
46    pandoc = os.path.realpath(which("pandoc"))
47except TypeError as e:
48    pandoc = None
49
50try:
51    texinfo = os.path.realpath(which("texinfo"))
52except TypeError as e:
53    texinfo = None
54
55docsrc = glob.glob('doc/src/**/*', recursive=True)
56
57for srcdoc in docsrc:
58    process = subprocess.Popen([emacs, srcdoc, "--batch", "-f",
59                                "org-texinfo-export-to-texinfo", "--kill"],
60                                stdout=subprocess.PIPE)
61    procom = process.communicate()
62
63doctexi1 = glob.glob('doc/src/**/*.texi', recursive=True)
64doctexi2 = []
65doctexi3 = []
66
67for texi in doctexi1:
68    doctexi2.append(os.path.realpath(texi))
69
70for texdoc in doctexi2:
71    newtex = texdoc.replace("doc/src/", "doc/texinfo/")
72    doctexi3.append(newtex)
73    with open(texdoc, "r") as f:
74        badtex = f.read()
75    goodtex = badtex.replace("@documentencoding UTF-8\n",
76                             "@documentencoding utf-8\n")
77    with open(newtex, "w") as f:
78        f.write(goodtex)
79
80for srcdoc in docsrc:
81    rstdoc = "{0}.rst".format(srcdoc.replace("doc/src/", "doc/rst/"))
82    process = subprocess.Popen([pandoc, "-f", "org", "-t", "rst+smart", "-o",
83                                rstdoc, srcdoc], stdout=subprocess.PIPE)
84    procom = process.communicate()
85
86with open("doc/rst/index.rst", "r") as f:
87    genindex = f.readlines()
88
89indextop = ['.. GPGME Python Bindings documentation master file, created by\n',
90            '   sphinx-quickstart on Wed Dec  5 09:04:47 2018.\n',
91            '   You can adapt this file completely to your liking, but it should at least\n',
92            '   contain the root `toctree` directive.\n', '\n',
93            'GPGME Python Bindings\n', '=====================\n', '\n',
94            '.. toctree::\n', '   :maxdepth: 3\n', '   :caption: Contents:\n',
95            '\n']
96
97with open("doc/rst/index.rst", "w") as f:
98    for line in indextop:
99        f.write(line)
100    for line in genindex[5:]:
101        f.write(line)
102
103with open("doc/rst/Makefile", "w") as f:
104    f.write("""# Minimal makefile for Sphinx documentation
105#
106
107# You can set these variables from the command line.
108SPHINXOPTS    =
109SPHINXBUILD   = sphinx-build
110SOURCEDIR     = .
111BUILDDIR      = _build
112
113# Put it first so that "make" without argument is like "make help".
114help:
115        @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
116
117.PHONY: help Makefile
118
119# Catch-all target: route all unknown targets to Sphinx using the new
120# "make mode" option.  $(O) is meant as a shortcut for $(SPHINXOPTS).
121%: Makefile
122        @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
123""")
124
125info_path = os.path.realpath(sysconfig._PREFIX + "/share/info")
126info_paths = os.environ["INFOPATH"].split(":")
127
128if info_paths.count(info_path) == 0:
129    info_paths.insert(0, info_path)
130else:
131    pass
132
133for ipath in info_paths:
134    if os.path.exists(os.path.realpath(ipath)) is False:
135        info_paths.remove(ipath)
136    else:
137        pass
138
139# Remove the old generated .texi files from the org source directory.
140for texifile in doctexi2:
141    os.remove(texifile)
142
143print("""
144You may now build your preferred documentation format using either:
145
146 1. Sphinx in the doc/rst/ directory; and/or
147 2. Texinfo or Makeinfo in the doc/texinfo/ directory.
148
149Alternatively the original Org mode source files can be found in the doc/src/
150directory.
151""")
152