Class: Solutus::Solutus_Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/solutus.rb

Defined Under Namespace

Classes: ServerStache

Constant Summary collapse

CSS =
File.read(File.join(File.dirname(File.expand_path(__FILE__)), '../resources', "style.css"))

Instance Method Summary collapse

Instance Method Details

#blog_selectObject



391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/solutus.rb', line 391

def blog_select
    result = "<select name=\"post-path\">\n"
    Solutus.blog_urls.each do |yr, arr|
        sorted = arr.sort_by {|k| k["date"]} .reverse
        sorted.each do |blog|
            title = blog["title"]
            path = blog["file_path"]
            url = blog["url"]
            result += "<option value=\"#{path},#{url}\">#{title}</option>\n"
        end
    end
    result += "</select>"
    result
end

#page_selectObject



362
363
364
365
366
367
368
369
370
371
# File 'lib/solutus.rb', line 362

def page_select
    result = "<select name=\"page-path\">\n"
    Solutus.page_urls.each do |page|
        path = page["file_path"]
        title = page["title"]
        result += "<option value=\"#{path}\">#{title}</option>\n"
    end
    result += "</select>"
    result
end

#path_to_resourcesObject



70
71
72
# File 'lib/solutus.rb', line 70

def path_to_resources
    File.join(File.dirname(File.expand_path(__FILE__)), '../resources')
end

#render_edit_file(path) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/solutus.rb', line 255

def render_edit_file(path)
    #TODO: do smth diff for YML files...
    ServerStache.template = File.read(File.join(path_to_resources, "editFile.html"))
    result = ServerStache.new
    result[:content] = File.read(path)
    result[:path] = path
    result.render
end

#render_edit_page(path) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/solutus.rb', line 268

def render_edit_page(path)
    f = File.open(File.join(path_to_resources, "editPage.html"), "r")
    template = ""
    f.each_line do |line|
        template += line
    end
    f.close

    f = File.open(path, "r")
    file_contents = ""
    f.each_line do |line|
        file_contents += line
    end
    data = YAML.load(file_contents)

    ServerStache.template = template
    result = ServerStache.new
    
    result[:css] = CSS
    result[:title] = data["title"]
    result[:path] = path
    link = path.split("/")[-1].split(".")[0]
    if ["error", "index"].include?(link)
        link = link + ".html"
    end
    result[:link] = link
    settings.global_settings.each do |key, val|
        result[key.to_sym] = val
    end
    page_html = Solutus.render_page(Solutus.templates, settings.global_settings, data)
    parser = Nokogiri::HTML(page_html)
    parser.at_css("#solutus-content")['contenteditable'] = true
    parser.at_css("#solutus-content")['style'] = "border: 1px black dashed" 
    result[:everything] = parser.at_css("#solutus-everything")
    result.render 
end

#render_edit_post(path, url = "/") ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/solutus.rb', line 305

def render_edit_post(path, url="/")
    f = File.open(File.join(path_to_resources, "editBlog.html"), "r")
    template = ""
    f.each_line do |line|
        template += line
    end
    f.close

    SolutusStache.template = template
    result = SolutusStache.new
    
    f = File.open(path, "r")
    file_contents = ""
    f.each_line do |line|
        file_contents += line
    end
    data = YAML.load(file_contents)

    result[:css] = CSS
    result[:title] = data["title"]
    result[:date] = data["date"].strftime(BLOG_DATE_FORMAT)
    result[:markdown] = file_contents.split("---")[-1]
    result[:link] = url
    result[:canview] = url != "/"
    result[:path] = path
    result[:subtitle] = data["subtitle"]
    result.render
end

#render_filesObject



373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/solutus.rb', line 373

def render_files 
    result = "<select name=\"path\">\n"
    dirs = [".", "static/css", "templates", "pages/site-pages"]
    dirs.each do |dir|
        result += "<optgroup label='#{dir}'>"
        files = Dir.entries(dir).select {|f| !File.directory? f}
        files.each do |filename|
            if !['password.txt', '.gitignore', 'nohup.out', 'log'].include?(filename)
                path = File.join(dir, filename)
                result += "<option value=\"#{path}\">#{filename}</option>\n"
            end
        end
        result += "</optgroup>"
    end
    result += "</select>"
    result
end

#render_indexObject



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/solutus.rb', line 342

def render_index
    f = File.open(File.join(path_to_resources, "index.html"), "r")
    template = ""
    f.each_line do |line|
        template += line
    end
    f.close
    SolutusStache.template = template
    result = SolutusStache.new
    settings.global_settings.each do |key, val|
        result[key.to_sym] = val
    end
    result[:blogs] = blog_select
    result[:pages] = page_select
    result[:css] = CSS
    result[:files] = render_files
    result[:version] = VERSION
    result.render
end

#render_login(message = "") ⇒ Object



334
335
336
337
338
339
340
# File 'lib/solutus.rb', line 334

def (message="")
    ServerStache.template = File.read(File.join(path_to_resources, "login.html"))
    result = ServerStache.new
    result[:message] = message
    result[:css] = CSS
    result.render
end

#send_404Object



264
265
266
# File 'lib/solutus.rb', line 264

def send_404
    send_file File.join(path_to_resources, "404.html")
end

#write_log(stuff) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/solutus.rb', line 243

def write_log(stuff)
    times = Time.now.getlocal('-07:00')
    if !File.file?(LOG_FILE)
        f = File.new(LOG_FILE, 'w')
        f.write("#{times} (PST) Creating log file\n")
        f.close
    end
    f = File.open(LOG_FILE, 'a')
    f.write("#{times} #{stuff}\n")
    f.close
end