Class: Jekyll::SideNotes::SideNoteGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-sidenotes.rb

Overview

verify all entries end with a "\n" so sidenotes works (sidenotes don't detect the last definition if there is no ending "\n").

Constant Summary collapse

LETTER =
'[:alpha:]'
COMBININGCHAR =

TODO

''
EXTENDER =

TODO

''
NCNAME_STR =
"[#{LETTER}_][-[:alnum:]._#{COMBININGCHAR}#{EXTENDER}]*"
UNAME_STR =
"(?:#{NCNAME_STR}:)?#{NCNAME_STR}"
BLANK_LINE =

from kramdown: https://github.com/gettalong/kramdown from blank_line.rb

/(?>^\s*\n)+/
EOB_MARKER =

from eob.rb

/^\^\s*?\n/
OPT_SPACE =

from kramdown.rb

/ {0,3}/
INDENT =

Regexp for matching indentation (one tab or four spaces)

/^(?:\t| {4})/
ALD_ID_CHARS =

from extensions.rb

/[\w-]/
ALD_ANY_CHARS =
/\\\}|[^\}]/
ALD_ID_NAME =
/\w#{ALD_ID_CHARS}*/
IAL_BLOCK =
/\{:(?!:|\/)(#{ALD_ANY_CHARS}+)\}\s*?\n/
IAL_BLOCK_START =
/^#{OPT_SPACE}#{IAL_BLOCK}/
HTML_SPAN_ELEMENTS =

from html.rb Some HTML elements like script belong to both categories (i.e. are valid in block and span HTML) and don't appear therefore! script, textarea

%w[a abbr acronym b big bdo br button cite code del dfn em i img input
ins kbd label mark option q rb rbc rp rt rtc ruby samp select small
span strong sub sup tt u var]
LAZY_END_HTML_SPAN_ELEMENTS =

from paragraph.rb

HTML_SPAN_ELEMENTS + %w[script]
LAZY_END_HTML_START =
/<(?>(?!(?:#{LAZY_END_HTML_SPAN_ELEMENTS.join('|')})\b)#{REXML::Parsers::BaseParser::UNAME_STR})/
LAZY_END_HTML_STOP =
/<\/(?!(?:#{LAZY_END_HTML_SPAN_ELEMENTS.join('|')})\b)#{REXML::Parsers::BaseParser::UNAME_STR}\s*>/m
CODEBLOCK_MATCH =

from markdown.rb CODEBLOCK_MATCH = /(?:#BLANK_LINE?(?:#INDENT[ \t]\S.\n)+)/ because .gsub(INDENT, '') in footnote.rb CODEBLOCK_MATCH = /(?:#BLANK_LINE?(?:s[ \t]\S.\n)+)/ from codeblock.rb

/(?:#{BLANK_LINE}?(?:#{INDENT}[ \t]*\S.*\n)+(?:(?!#{IAL_BLOCK_START}|#{EOB_MARKER}|^#{OPT_SPACE}#{LAZY_END_HTML_STOP}|^#{OPT_SPACE}#{LAZY_END_HTML_START})^[ \t]*\S.*\n)*)*/
RIGHT_SIDENOTE_DEFINITION_START =

footnotes (for reference) FOOTNOTE_DEFINITION_START = /^#OPT_SPACE[^(#ALD_ID_NAME)]:\s*?(.*?\n#CODEBLOCK_MATCH)/ FOOTNOTE_MARKER_START = /[^(#ALD_ID_NAME)]/

constants for local use

right

/^#{OPT_SPACE}\[\>(#{ALD_ID_NAME})\]:\s*?(.*?\n#{CODEBLOCK_MATCH})/
RIGHT_SIDENOTE_MARKER_START =
/\[\>(#{ALD_ID_NAME})\]/
LEFT_SIDENOTE_DEFINITION_START =

left

/^#{OPT_SPACE}\[\<(#{ALD_ID_NAME})\]:\s*?(.*?\n#{CODEBLOCK_MATCH})/
LEFT_SIDENOTE_MARKER_START =
/\[\<(#{ALD_ID_NAME})\]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#md_docsObject

Returns the value of attribute md_docs.



16
17
18
# File 'lib/jekyll-sidenotes.rb', line 16

def md_docs
  @md_docs
end

Instance Method Details

#generate(site) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll-sidenotes.rb', line 77

def generate(site)

  # setup markdown docs
      docs = site.pages + site.docs_to_write
      @md_docs = docs.filter {|doc| site.find_converter_instance(Jekyll::Converters::Markdown).matches(doc.extname) }
  
  link_extension = !!site.config["use_html_extension"] ? '.html' : ''
     
  @md_docs.each do |cur_doc|
    # check for newlines @ eof.
    #   (kramdown can handle footnotes with no newline, but the regex i'm getting requires a newline after the last footnote to find it.)
    if cur_doc.content[-1] != "\n"
      Jekyll.logger.debug "Missing newline at end of file -- this could break sidenotes: ", cur_doc.data['title']
    end    
    parse_sidenote(cur_doc, "left")
    parse_sidenote(cur_doc, "right")
  end
end

#parse_sidenote(doc, side) ⇒ Object

mark -> [<left-sidenote], [>right-sidenote] def -> [<left-sidenote]:, [>right-sidenote]: side should be 'right' or 'left'



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
# File 'lib/jekyll-sidenotes.rb', line 102

def parse_sidenote(doc, side)
  # left v right setup
  if side == "right"
    sidenote_def_regex = RIGHT_SIDENOTE_DEFINITION_START
    # sidenote_mark_regex = RIGHT_SIDENOTE_MARKER_START
    css_class = "rsn"
    sn_regex = /\>/
  elsif side == "left"
    sidenote_def_regex = LEFT_SIDENOTE_DEFINITION_START
    # sidenote_mark_regex = LEFT_SIDENOTE_MARKER_START
    css_class = "lsn"
    sn_regex = /\</
  else
      Jekyll.logger.warn "Can't process sidenote that is not either 'right' or 'left'."
      return
  end
  # process sidenotes
  sidenotes = doc.content.scan(sidenote_def_regex)
  doc.content.gsub!(sidenote_def_regex, '') # rm sidenote defs from original note.
  i = 0
  sidenotes.each do |sidenote|
    i += 1
    mark = sidenote[0]
    definition = sidenote[1]
    doc.content = doc.content.gsub(
      /\[#{sn_regex}(#{mark})\]/i,
      "<label for=\"#{css_class}-#{i}\" class=\"sidenote-toggle sidenote-number\"></label><input type=\"checkbox\" id=\"#{css_class}-#{i}\" class=\"sidenote-toggle\"><span class=\"#{css_class}\">#{definition}</span>"
    )
  end
end