1#!/usr/bin/env python
2#
3#   Copyright 2013 Pixar
4#
5#   Licensed under the Apache License, Version 2.0 (the "Apache License")
6#   with the following modification; you may not use this file except in
7#   compliance with the Apache License and the following modification to it:
8#   Section 6. Trademarks. is deleted and replaced with:
9#
10#   6. Trademarks. This License does not grant permission to use the trade
11#      names, trademarks, service marks, or product names of the Licensor
12#      and its affiliates, except as required to comply with Section 4(c) of
13#      the License and to reproduce the content of the NOTICE file.
14#
15#   You may obtain a copy of the Apache License at
16#
17#       http://www.apache.org/licenses/LICENSE-2.0
18#
19#   Unless required by applicable law or agreed to in writing, software
20#   distributed under the Apache License with the above modification is
21#   distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22#   KIND, either express or implied. See the Apache License for the specific
23#   language governing permissions and limitations under the Apache License.
24#
25
26import os
27import sys
28import string
29import re
30
31#-------------------------------------------------------------------------------
32def ReadFile(inputfile):
33    try:
34        f = open( inputfile, "r")
35    except IOError:
36        print("Could not read file \'"+inputfile+"\'")
37    content = f.read()
38    f.close()
39    return content
40
41#-------------------------------------------------------------------------------
42def WriteToFile(outputfile, content):
43    outputPath = os.path.dirname(outputfile)
44    try:
45        os.makedirs(outputPath);
46    except:
47        pass
48    try:
49        f = open(outputfile, "w")
50    except IOError:
51        print("Could not write file \'"+outputfile+"\'")
52    f.write(content)
53    f.close()
54
55#-------------------------------------------------------------------------------
56# Reformats the C++ tutorial file as a ReST file for publication on the
57# documentation site
58def Process(srcfile, title):
59
60    basename = os.path.basename(srcfile)
61
62    rest = "\n"
63
64    rest += basename+"\n"
65    rest += ("-" * len(basename))+"\n\n"
66
67    rest += "`<https://github.com/PixarAnimationStudios/OpenSubdiv/blob/release/tutorials/"+title+">`_\n"
68
69    rest += ("\n"
70             "----\n"
71             "\n"
72             ".. code:: c\n")
73
74    code = ReadFile(srcfile)
75
76    lines = code.split('\n')
77    # cut license header (line 24)
78    for line in lines[24:]:
79        rest += '    ' + line + '\n'
80
81    #lines = [i.start() for i in re.finditer("\n", code)]
82    #rest += code[lines[25]:]
83
84    return rest
85
86#-------------------------------------------------------------------------------
87def Usage():
88    print(str(sys.argv[0])+" <input file> <output file> <title>")
89    exit(1);
90
91
92#-------------------------------------------------------------------------------
93# Main
94
95# XXXX manuelk we should have this script traverse the tutorials folders and
96#              automatically generate both the ReST from the C++ code as well
97#              as the tutorials.rst file based on what is found
98if (len(sys.argv)!=4):
99    Usage()
100
101srcfile = str(sys.argv[1])
102title = str(sys.argv[3])
103rest = Process(srcfile, title)
104
105dstfile = str(sys.argv[2])
106WriteToFile(dstfile, rest)
107