viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. import re, subprocess, os, tempfile, shutil
  2.  
  3. class latex_manager():
  4. def __init__(self):
  5. ## TODO: Verify Latex Installation
  6. return
  7.  
  8. def create_song(self, song):
  9. # Ci aspettiamo di ottenere un oggetto song
  10. title = song.title
  11. tone = song.tone
  12. mauthor = song.mauthor
  13. tauthor = song.tauthor
  14. year = song.year
  15. body = song.body
  16.  
  17. # We really have to assume that latex_song is unicode,
  18. # or the write() function will eat us!
  19. latex_song = unicode()
  20. latex_song += "\\begin{song}{" + title + "}{" + tone + "}\n{" + mauthor + "}\n{" + tauthor + "}\n{" + year + "}" + "{}\n\n\\index{" + title + "}\n\n"
  21.  
  22. for item in body:
  23. if(item.is_chorus()):
  24. latex_song += "\n\\begin{SBChorus}\n"
  25. latex_song += re.sub("\n", "\n\n", item.content())
  26. latex_song += "\n\\end{SBChorus}\n"
  27. else:
  28. latex_song += "\n\\begin{SBVerse}"
  29. latex_song += re.sub("\n" , "\n\n", item.content())
  30. latex_song += "\n\\end{SBVerse}\n"
  31.  
  32. latex_song += "\n\\end{song}\n\n"
  33.  
  34. # Transform chords in LaTeX Chords (this part has to be removed..)
  35. #latex_song = re.sub("\[", "\\Ch{", latex_song)
  36. #latex_song = re.sub("\](\w|\s){4}", self.quadra_to_chord_end, latex_song)
  37. #latex_song = re.sub("\]", "}{}", latex_song)
  38.  
  39. latex_song = self.sub_chord(latex_song)
  40.  
  41. return latex_song
  42.  
  43.  
  44. def sub_chord(self,m):
  45. j = 0
  46. # print j, len(m) this is just debug!
  47. while(j < len(m)):
  48. if(m[j] == '['):
  49. # We have a chord!, then...
  50. # 1) How many letters is the chords?
  51. count = 0
  52. while(m[count + j + 1] != ']'):
  53. count += 1
  54. # 2) Are there count + 1 words "free" after the chord?
  55. free = count + 2
  56. for i in range(j + count + 2,j + count + 2 + (count + 2) ):
  57. # print "m[i] = ", m[i] Removing debug
  58. if( (m[i] == '\n') | (m[i] == '[') | (m[i] == '{') ):
  59. free = i - j - count - 2
  60. break
  61. # Debug is unuseful now! print "free = " , free
  62. # Free what we need...and rebuild the new string...
  63. m = m[0:j] + "\Ch{" + m[j+1:j+count+1] + "}{" + m[j+count+2:j+count+free+2] + "}" + m[j+count+free+2:len(m)]
  64. j = j + count + free + 3
  65. else:
  66. j += 1
  67. return m
  68.  
  69. def export_songbook(self,song_list, opt):
  70. # Assume that song list is an array of songs and
  71. # create a songbook with them
  72.  
  73. # New buffer
  74. buf = unicode()
  75.  
  76. # TODO: Latex code to compile the songbook
  77. # Document class
  78. buf += "\\documentclass[10pt," + opt["paper_size"] + ",twoside]{book}\n"
  79.  
  80. # Packages
  81. buf += "\\usepackage[" + opt["paper_size"] + "paper, " + opt["type"] + "]{songbook}\n"
  82. buf += "\\usepackage[utf8x]{inputenc}\n"
  83. buf += "\\usepackage{makeidx}\n"
  84. buf += "\n\n"
  85.  
  86. # Index generation
  87. buf += "\index{" + opt["title"] + "}\n"
  88. buf += "\\makeindex\n"
  89. buf += "\n\n"
  90.  
  91. # Document begins
  92. buf += "\\begin{document}\n\n"
  93.  
  94. # Main title
  95. buf += "\\title{" + opt["title"] + "}"
  96.  
  97. for song in song_list:
  98. buf += "\n\n" # Put some space between songs
  99. buf += "% Canzone:" + song.title + "\n\n"
  100. buf += self.create_song(song)
  101.  
  102. # The Index
  103. buf += "\printindex\n"
  104.  
  105. # Document ends
  106. buf += "\end{document}\n\n"
  107.  
  108. # Give buffer back to be printed
  109. return buf
  110.  
  111. def latex_compile(self, latex_file, opt):
  112. # We should check that latex exist...
  113. lat = subprocess.Popen(["latex", ""], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  114.  
  115. # Gli spediamo il nostro documento latex
  116. out, err = lat.communicate(latex_file.encode("utf-8"))
  117.  
  118. return out # Binary data
  119.  
  120. def create_index_ist(self):
  121. buf = unicode();
  122. buf += "headings_flag 1\n"
  123. buf += "heading_prefix \"\\n \\\\item \\\\textbf{\"\n"
  124. buf += "heading_suffix \"}\" \n"
  125. buf += "symhead_positive \"Simboli\"\n"
  126. buf += "symhead_negative \"simboli\"\n"
  127. buf += "numhead_positive \"Numeri\"\n"
  128. buf += "numhead_negative \"numeri\"\n"
  129. buf += "delim_0 \" \\\\dotfill\\\\ \"\n"
  130. buf += "delim_1 \" \\\\dotfill\\\\ \"\n"
  131. buf += "delim_2 \" \\\\dotfill\\\\ \"\n"
  132. return buf
  133.  
  134.  
  135. def create_pdf_from_songbook(self, song_list, opt):
  136. # Creo una directory temporanea
  137. tmpdir = tempfile.mkdtemp()
  138.  
  139. # Creo il file index.ist che mi servira' per l'indice
  140. f = open(tmpdir + "/index.ist", 'w')
  141. f.write(self.create_index_ist())
  142. f.close()
  143.  
  144. # Creo il file latex
  145. f = open(tmpdir + "/canzoniere.tex", 'w')
  146. f.write(self.export_songbook(song_list, opt).encode("utf-8"))
  147. f.close()
  148.  
  149. # Comincio a compilare il codice
  150. p = subprocess.Popen(["latex", tmpdir + "/canzoniere.tex"], cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  151. out, err = p.communicate("q\n") # in questo modo va avanti anche in caso di errori
  152. p.wait()
  153.  
  154. # print tmpdir # Se gia' fa questo non e' male :)
  155.  
  156. # Creo l'indice
  157. p = subprocess.Popen("makeindex -s index.ist canzoniere.idx", shell=True ,cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  158. p.wait()
  159.  
  160. # Ricreo il DVI finale
  161. p = subprocess.Popen(["latex", tmpdir + "/canzoniere.tex"], cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  162. out, err = p.communicate("q\n") # in questo modo va avanti anche in caso di errori
  163. p.wait()
  164.  
  165. # Converto in PS
  166. p = subprocess.Popen("dvips -t " + opt["paper_size"] + " canzoniere.dvi -q -o", shell=True, cwd = tmpdir, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
  167. p.wait()
  168.  
  169. # Converto in PDF
  170. p = subprocess.Popen(["ps2pdf", "canzoniere.ps"], cwd = tmpdir, stdin = subprocess.PIPE, stdout =subprocess.PIPE, stderr= subprocess.PIPE)
  171. p.wait()
  172.  
  173. # Leggo il PDF
  174. f = open(tmpdir + "/canzoniere.pdf", 'rb')
  175. pdf = f.read()
  176. f.close()
  177.  
  178. # Elimino le cartelle temporanee
  179. shutil.rmtree(tmpdir)
  180.  
  181. return pdf
  182.  
  183.  
  184.  
  185.