Class: KeepYourHead::Database::StyleLatex

Inherits:
Style
  • Object
show all
Defined in:
lib/Keepyourhead/style/StyleLatex.rb

Constant Summary collapse

WidthInCm =
20

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Style

addStyle, getStyle

Constructor Details

#initialize(file) ⇒ StyleLatex

Returns a new instance of StyleLatex.



29
30
# File 'lib/Keepyourhead/style/StyleLatex.rb', line 29

def initialize( file )
end

Class Method Details

.NameObject



25
26
27
# File 'lib/Keepyourhead/style/StyleLatex.rb', line 25

def self.Name
	"latex"
end

Instance Method Details

#compileLatex(baseDirectory, content_latex, options) ⇒ Object



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
161
162
163
164
165
# File 'lib/Keepyourhead/style/StyleLatex.rb', line 88

def compileLatex(baseDirectory, content_latex, options )
	width = options[:width] || Gdk::Screen.default.width || 10
	
	
	baseDirectory = ::File.expand_path baseDirectory

	directory = Resources::createWorkingDirectory
	compilation = nil
		
	success = true

	filename_tex = "flashcard.tex"
	filename_output = "output.txt"
	filename_tex = ::File.expand_path( filename_tex, directory )
	filename_output = ::File.expand_path( filename_output, directory )

	file_tex = ::File.new(filename_tex, "w")
	file_tex << content_latex
	file_tex.close

	filename_png = 'flashcard_%d.png'
	filename_png = ::File.expand_path filename_png, directory


	sub_id = fork { # fork because of use of FileUtils.cd and concurrency
		success = true
		begin
			Process.setpriority(Process::PRIO_PROCESS, 0, 19)

			FileUtils.cd( directory ) do |dir|

				# latex execution
				FileUtils.cd( baseDirectory ) {
					system( "pdflatex -output-directory='#{directory}' -interaction nonstopmode '#{filename_tex}' >/dev/null")
					success &&= system( "pdflatex -output-directory='#{directory}' -interaction nonstopmode -halt-on-error '#{filename_tex}' 2>&1 >#{filename_output}")
				}

				# ghostscript execution
				dpi = (width / (WidthInCm / 2.54)).to_i

				success &&= system( "gs -dSAFER -dBATCH -dNOPAUSE -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -sDEVICE=png16m -sOutputFile='#{filename_png}' -r#{dpi} flashcard.pdf >> #{filename_output}")
			end
		rescue
			print $!, $!.backtrace, "---"
			success = false
		end
		
		exit! success ? 1 : 0
	}

	pid, status = Process.wait2 sub_id
	success &&= (status.exitstatus == 1)

	filenames = []
	i = 0
	loop do
		filename = filename_png.sub( '%d', i.to_s )
		if ::File.exist? filename then
			filenames << filename 
		else
			break if i > 5
		end

		i += 1
	end

	file_output = ::File.new( filename_output, "r" )
	output = file_output.readlines.join("")
	::File.delete filename_output

	compilation = Compilation.new content_latex, success, output, directory, filenames
	
	if not compilation.success then
		compilation.remove
	end

	compilation
end

#content(type, flashcard, options = {}) ⇒ Object



38
39
40
# File 'lib/Keepyourhead/style/StyleLatex.rb', line 38

def content(type,flashcard, options = {})
	latex_code( type, flashcard, options )
end

#createImage(type, flashcard, options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/Keepyourhead/style/StyleLatex.rb', line 32

def createImage(type, flashcard, options = {})
	latex = latex_code( type, flashcard, options )
	
	compileLatex ::File.dirname(flashcard.file.filename), latex, options
end

#latex_code(type, flashcard, options = {}) ⇒ Object



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
# File 'lib/Keepyourhead/style/StyleLatex.rb', line 45

def latex_code( type, flashcard, options = {} )
	previewCode = options[:previewCode]
	width = options[:width] || Gdk::Screen.default.width || 10

	assert( type == FRONT || type == BACK )
	ret = case type
		when FRONT
			FrontScheme
		when BACK
			BackScheme
		end.clone

	topics = flashcard.topicNames

	contentFront = flashcard.front
	contentFront ||= topics.last
	contentBack = flashcard.back

	if previewCode then
		case type
		when FRONT
			contentFront = previewCode
		when BACK
			contentBack = previewCode
		end
	end
	
	ret.gsub!( "$(widthInCm)", WidthInCm.to_s )

	ret.gsub!( "$(width)", width.to_s )
	ret.gsub!( "$(header)", flashcard.collection.latexHeader || "" )
	ret.gsub!( "$(type)", flashcard.type || "" )
	ret.gsub!( "$(name)", flashcard.name || "" )
	ret.gsub!( "$(content_front)", contentFront || "" )
	ret.gsub!( "$(content_back)", contentBack || "" )
	ret.gsub!( "$(topics)", topics.join(".") )
	ret.gsub!( "$(prepend)", flashcard.collection.latexPrepend || "" )
	ret.gsub!( "$(append)", flashcard.collection.latexAppend || "" )


	ret
end