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"))
FILE_EXCLUDES =
['password.txt', '.gitignore', 'nohup.out', 'log']

Instance Method Summary collapse

Instance Method Details

#blog_selectObject



443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/solutus.rb', line 443

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



408
409
410
411
412
413
414
415
416
417
# File 'lib/solutus.rb', line 408

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



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

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

#render_edit_file(path) ⇒ Object



288
289
290
291
292
293
294
295
# File 'lib/solutus.rb', line 288

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



301
302
303
304
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
333
334
335
336
# File 'lib/solutus.rb', line 301

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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/solutus.rb', line 338

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



419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/solutus.rb', line 419

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

#render_indexObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/solutus.rb', line 375

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
    dirs = Dir.entries(SITE_PAGES_PATH)
    puts dirs
    dir_res = ''
    dirs.each do |d|
        if File.directory?(File.join(SITE_PAGES_PATH, d)) && d != ".."
            val = d + "/"
            if val == '.'
                val = "/"
            end
            dir_res += "<option value='#{d}'>#{val}</option>"
        end
    end
    result[:page_paths] = dir_res
    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



367
368
369
370
371
372
373
# File 'lib/solutus.rb', line 367

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



297
298
299
# File 'lib/solutus.rb', line 297

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

#write_log(stuff) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
# File 'lib/solutus.rb', line 276

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