Module: Lines::ApplicationHelper

Defined in:
app/helpers/lines/application_helper.rb

Defined Under Namespace

Classes: HTMLwithPygments

Instance Method Summary collapse

Instance Method Details

#display_article_authors(article, with_info = false) ⇒ Object

Returns HTML with all authors of an article



54
55
56
57
58
59
60
# File 'app/helpers/lines/application_helper.rb', line 54

def (article, with_info=false)
  authors = article.authors.map{|author| author.gplus_profile.blank? ? author.name : link_to(author.name, author.gplus_profile)}.to_sentence(two_words_connector: " & ", last_word_connector: " & ").html_safe
  if with_info
    authors += (" - " + article.authors.map{|author| (:span, "#{author.description}", class: 'author_description') }.join(" -- ")).html_safe
  end
  authors
end

#format_code(text) ⇒ Object

Returns HTML for code blocks formatted with Pygment



100
101
102
# File 'app/helpers/lines/application_helper.rb', line 100

def format_code(text)
  simple_format( truncate( Sanitize.clean(markdown(text)), length: 300, separator: ' ', omission: ' ...' ))
end

Returns site name for actionbar, dependend on current site



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/lines/application_helper.rb', line 81

def get_action_link
  if controller_path == 'admin/articles'
    case action_name
      when 'index' then 'All articles'
      when 'new' then 'New article'
      when 'edit' then 'Edit article'
      when 'show' then "Preview"
    end
  elsif controller_path == 'admin/authors'
    case action_name
      when 'index' then 'All authors'
      when 'new' then 'New author'
      when 'edit' then 'Edit author'
      when 'show' then "Author"
    end
  end
end

#markdown(text) ⇒ Object

Returns formatted and highlighted code fragments



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/helpers/lines/application_helper.rb', line 29

def markdown(text)
  renderer = HTMLwithPygments.new(hard_wrap: true, filter_html: false, with_toc_data: false)
  options = {
    autolink: true,
    no_intra_emphasis: true,
    fenced_code_blocks: true,
    lax_html_blocks: true,
    tables: true,
    strikethrough: true,
    superscript: true,
    xhtml: true
  }
  Redcarpet::Markdown.new(renderer, options).render(text).html_safe
end

Returns links in active or inactive state for highlighting current page



45
46
47
48
49
50
51
# File 'app/helpers/lines/application_helper.rb', line 45

def nav_link(link_text, link_path)
  recognized = Rails.application.routes.recognize_path(link_path)
  class_name = recognized[:controller] == params[:controller] ? 'active' : ''
  (:li, class: class_name) do
    link_to link_text, link_path
  end
end

#render_navbar(&block) ⇒ Object

Renders the navigation bar for logged in users



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/lines/application_helper.rb', line 63

def render_navbar(&block)
  action_link = get_action_link
  if !action_link
    action_link = CONFIG[:title_short]
  end
  html = (:div, id: 'navbar') do
    (:div, class: 'navbar-inner') do
      if current_lines_user
        (:span, link_to('', admin_articles_path), class: 'backlink', title: 'Dashboard') + (:span, action_link, class: 'actionlink') + (:span, class: 'buttons', &block) + (:span, link_to('Logout', logout_path), class: 'logout') + (:span, "Logged in as #{current_lines_user.email}", class: 'logged-in-as')
      else
        (:span, link_to('', lines.root_path), class: 'backlink') + (:span, action_link, class: 'actionlink')
      end
    end
  end
  html    
end

#render_teaser(article, article_counter = 0) ⇒ Object

Renders the teaser for an article.



9
10
11
12
13
14
15
16
# File 'app/helpers/lines/application_helper.rb', line 9

def render_teaser(article, article_counter=0)
  if article_counter < 0
    teaser = article.teaser.present? ? markdown(article.teaser) : nil
  else
    teaser = article.teaser.present? ? format_code(article.teaser) : format_code(article.content)
  end
  teaser
end