Module: Jekyll::CodeExampleTags

Defined in:
lib/jekyll-code-example-tag.rb

Defined Under Namespace

Classes: AllPageCodeExamplesTag, CodeExampleTag, CodeExamplesJsFile, CodeExamplesJsGenerator

Class Method Summary collapse

Class Method Details

.buttons_markup(examples) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jekyll-code-example-tag.rb', line 31

def self.buttons_markup(examples)
  menu_items = ""
  examples.each_key do |lang|
    menu_items << "<li><a href='#' class='button' target='#{lang}'>#{lang.capitalize}</a></li>"
  end
  <<EOF
        <div class="buttons examples">
          <ul>
            #{menu_items}
          </ul>
        </div>
EOF
end

.code_example_dir(site) ⇒ Object



4
5
6
# File 'lib/jekyll-code-example-tag.rb', line 4

def self.code_example_dir(site) 
  site.fetch('code_example_dir', 'code_examples')
end

.code_examples(context_path, example_name, site) ⇒ Object

Returns a hash of available code examples (per language) for the provided example name



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-code-example-tag.rb', line 9

def self.code_examples(context_path, example_name, site)
  # Collect all relevant files
  examples_root = File.join(code_example_dir(site), context_path)

  code_folders = Dir.entries(examples_root).select do |entry|
    File.directory? File.join(examples_root, entry) and !(entry =='.' || entry == '..')
  end

  examples = {}
  code_folders.each do |lang|
    code_folder = File.join(examples_root, lang)
    example_file = Dir.entries(code_folder).find do |entry|
      File.file? File.join(code_folder, entry) and entry == example_name
    end
    if example_file
      examples[lang] = File.join(code_folder, example_file)
    end
  end

  examples
end

.example_markup(language, content) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/jekyll-code-example-tag.rb', line 45

def self.example_markup(language, content)
  <<EOF
      <div class="highlight example #{language}">
        <pre><code class="language #{language}" data-lang="#{language}">#{content}</code></pre>
      </div>
EOF

end

.get_example_name_and_context(example_string) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jekyll-code-example-tag.rb', line 58

def self.get_example_name_and_context(example_string)
  example_string.strip! 
  if example_string.include?('/')
    example_arr = example_string.split('/')
    example_name = example_arr.delete_at(-1)
    context_path = example_arr.join(File::SEPARATOR) + File::SEPARATOR 
  else
    context_path = ''
    example_name = example_string
  end

  return context_path, example_name
end

.wrap_examples_div(content) ⇒ Object



54
55
56
# File 'lib/jekyll-code-example-tag.rb', line 54

def self.wrap_examples_div(content)
  "<div class='code-examples'>#{content}</div>"
end