Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

#!/usr/local/bin/python 

# encoding: utf-8 

""" 

mmd.py 

=========== 

:Summary: 

    My MMD helpers 

 

:Author: 

    David Young 

 

:Date Created: 

    September 19, 2013 

 

:dryx syntax: 

    - ``_someObject`` = a 'private' object that should only be changed for debugging 

 

:Notes: 

    - If you have any questions requiring this script please email me: d.r.young@qub.ac.uk 

 

:Tasks: 

    @review: when complete add cl commands 

    @review: make internal function private 

    @review: pull all general functions and classes into dryxPython 

""" 

################# GLOBAL IMPORTS #################### 

import sys 

import os 

 

################################################################### 

# CLASSES                                                         # 

################################################################### 

 

################################################################### 

# PUBLIC FUNCTIONS                                                # 

################################################################### 

## LAST MODIFIED : September 19, 2013 

## CREATED : September 19, 2013 

## AUTHOR : DRYX 

def convert_to_html( 

        log, 

        pathToMMDFile, 

        css="amblin"): 

    """convert mmd file to html 

 

    **Key Arguments:** 

 

    **Return:** 

        - ``pathToHtmlFile`` -- the path to the html file 

 

    **Todo** 

        - @review: when complete, clean convert_to_html function 

        - @review: when complete add logging 

        - @review: when complete, decide whether to abstract function to another module 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    from subprocess import Popen, PIPE 

    ## THIRD PARTY ## 

    ## LOCAL APPLICATION ## 

 

    log.info('starting the ``convert_to_html`` function') 

    ## TEST THE ARGUMENTS 

 

    ## GRAB THE CSS ## 

    cssFile = os.path.dirname(__file__) + "/assets/%s.css" % (css,) 

    pathToReadFile = cssFile 

    try: 

        log.debug("attempting to open the file %s" % (pathToReadFile,)) 

        readFile = open(pathToReadFile, 'r') 

        cssData = readFile.read() 

    except IOError, e: 

        message = 'could not open the file %s' % (pathToReadFile,) 

        log.critical(message) 

        raise IOError(message) 

    readFile.close() 

 

 

    pathToHtmlFile=pathToMMDFile 

    for ext in [".md",".mmd",".markdown",".txt",".dat"]: 

        pathToHtmlFile = pathToHtmlFile.replace(ext,".html") 

 

    ## MAKE A HEADER IS WRITTEN 

    pathToReadFile = pathToMMDFile 

    try: 

        log.debug("attempting to open the file %s" % (pathToReadFile,)) 

        readFile = open(pathToReadFile, 'r') 

        thisData = readFile.read() 

    except IOError, e: 

        message = 'could not open the file %s' % (pathToReadFile,) 

        log.critical(message) 

        raise IOError(message) 

 

    readFile.close() 

    basename = os.path.basename(pathToMMDFile) 

    filenameNoExtension = os.path.splitext(basename)[0] 

    if ("Filename: `%s`" % (filenameNoExtension,)) not in thisData: 

        thisData = "Filename: %s\n\n%s" % (filenameNoExtension,thisData) 

        pathToWriteFile = pathToMMDFile 

        try: 

            log.debug("attempting to open the file %s" % (pathToWriteFile,)) 

            writeFile = open(pathToWriteFile, 'w') 

        except IOError, e: 

            message = 'could not open the file %s' % (pathToWriteFile,) 

            log.critical(message) 

            raise IOError(message) 

        writeFile.write(thisData) 

        writeFile.close() 

 

 

 

    mmdBinary = os.path.dirname(__file__) + "/assets/mmd" 

 

    process = Popen([mmdBinary, pathToMMDFile], stdout=PIPE) 

    stdout, stderr = process.communicate() 

 

    pathToReadFile = pathToHtmlFile 

    try: 

        log.debug("attempting to open the file %s" % (pathToReadFile,)) 

        readFile = open(pathToReadFile, 'r') 

        thisData = readFile.read() 

    except IOError, e: 

        message = 'could not open the file %s' % (pathToReadFile,) 

        log.critical(message) 

        raise IOError(message) 

 

    readFile.close() 

 

    thisData = thisData.replace('<body>','<style>%s</style><body><div id="wrapper">' % (cssData,)) 

    thisData = thisData.replace('</body>','</div></body>') 

    pathToWriteFile = pathToHtmlFile 

    try: 

        log.debug("attempting to open the file %s" % (pathToWriteFile,)) 

        writeFile = open(pathToWriteFile, 'w') 

    except IOError, e: 

        message = 'could not open the file %s' % (pathToWriteFile,) 

        log.critical(message) 

        raise IOError(message) 

    writeFile.write(thisData) 

    writeFile.close() 

 

    log.info('completed the ``convert_to_html`` function') 

    return pathToHtmlFile 

 

 

################################################################### 

# PRIVATE (HELPER) FUNCTIONS                                      # 

################################################################### 

 

############################################ 

# CODE TO BE DEPECIATED                    # 

############################################ 

 

if __name__ == '__main__': 

    main() 

 

 

################################################################### 

# TEMPLATE FUNCTIONS                                              # 

###################################################################