Skip to content

Commit

Permalink
merge PR #63
Browse files Browse the repository at this point in the history
resolves #55 sort attribute entries in document header
  • Loading branch information
mojavelinux authored Aug 19, 2018
2 parents beb0c28 + 06c1721 commit 4c7b61d
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 42 deletions.
19 changes: 10 additions & 9 deletions lib/kramdown-asciidoc/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,17 @@ def convert_heading el, opts
style << %(.#{role.tr ' ', '.'})
end
attrs.unshift style.join unless style.empty?
lines = []
lines << %([#{attrs.join ','}]) unless attrs.empty?
# NOTE kramdown removes newlines from heading
lines << %(#{'=' * level} #{compose_text el, strip: true})
attrlist = %([#{attrs.join ','}]) unless attrs.empty?
# NOTE kramdown has already removed newlines
title = compose_text el, strip: true
if level == 1 && writer.empty? && @current_heading_level != 1
writer.header.push(*lines)
writer.add_prologue_line attrlist if attrlist
writer.doctitle = title
nil
else
@attributes['doctype'] = 'book' if level == 1
writer.add_lines lines
writer.add_line attrlist if attrlist
writer.add_line %(#{'=' * level} #{title})
end
@current_heading_level = level unless discrete
nil
Expand Down Expand Up @@ -298,7 +299,7 @@ def convert_img el, opts
style << %(.#{role.tr ' ', '.'})
end
attrs.unshift style.join unless style.empty?
block_attributes_line = %([#{attrs.join ','}]) unless attrs.empty?
attrlist = %([#{attrs.join ','}]) unless attrs.empty?
block = true
end
macro_attrs = [nil]
Expand All @@ -325,7 +326,7 @@ def convert_img el, opts
writer = opts[:writer]
if block
writer.start_block
writer.add_line block_attributes_line if block_attributes_line
writer.add_line attrlist if attrlist
writer.add_line %(image::#{src}[#{macro_attrs.join ','}])
else
writer.append %(image:#{src}[#{macro_attrs.join ','}])
Expand Down Expand Up @@ -652,7 +653,7 @@ def extract_prologue el, opts
(prologue_el = el.dup).children = children.take_while {|child| child.type == :xml_comment || child.type == :blank }
(el = el.dup).children = children.drop prologue_el.children.size
traverse prologue_el, (opts.merge writer: (prologue_writer = Writer.new))
opts[:writer].header.push(*prologue_writer.body)
opts[:writer].add_prologue_lines prologue_writer.body
end
el
end
Expand Down
38 changes: 16 additions & 22 deletions lib/kramdown-asciidoc/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,30 @@ module Kramdown; module AsciiDoc
class Writer
LF = ?\n

attr_reader :header
attr_accessor :doctitle
attr_reader :body

def initialize
@header = []
@prologue = []
@doctitle = nil
@attributes = {}
@body = []
@nesting_stack = []
@block_delimiter = nil
@block_separator = ['']
@list_level = { list: 0, dlist: 0 }
end

def doctitle
if (doctitle_line = @header.find {|l| l.start_with? '= ' })
doctitle_line.slice 2, doctitle_line.length
end
end

def doctitle= new_doctitle
if (doctitle_idx = @header.index {|l| l.start_with? '= ' })
@header[doctitle_idx] = %(= #{new_doctitle})
else
@header.unshift %(= #{new_doctitle})
end
nil
def add_attributes new_attributes
@attributes.update new_attributes
end

def add_attributes attributes
attributes.each {|k, v| add_attribute k, v }
nil
def add_prologue_line line
@prologue << line
end

def add_attribute name, value
@header << %(:#{name}:#{value.empty? ? '' : ' ' + value.to_s})
nil
def add_prologue_lines lines
@prologue.push(*lines)
end

def start_block
Expand Down Expand Up @@ -125,7 +114,12 @@ def empty?
end

def to_s
(@header.empty? ? @body : (@header + (@body.empty? ? [] : [''] + @body))).join LF
header = @prologue.dup
header << %(= #{@doctitle}) if @doctitle
@attributes.sort.each do |name, val|
header << (val.empty? ? %(:#{name}:) : %(:#{name}: #{val}))
end unless @attributes.empty?
(header.empty? ? @body : (header + (@body.empty? ? [] : [''] + @body))).join LF
end
end
end; end
6 changes: 6 additions & 0 deletions spec/scenarios/attributes/sorting.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
= Document Title
:bar: bar
:experimental:
:foo: foo

menu:Terminal[Quit]
3 changes: 3 additions & 0 deletions spec/scenarios/attributes/sorting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Document Title

**Terminal > Quit**
3 changes: 3 additions & 0 deletions spec/scenarios/attributes/sorting.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:attributes:
foo: 'foo'
bar: 'bar'
2 changes: 1 addition & 1 deletion spec/scenarios/heading/lazy_ids/auto-ids.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
= Document Title
:idseparator: -
:idprefix:
:idseparator: -

== A Section

Expand Down
10 changes: 0 additions & 10 deletions spec/writer_spec.rb

This file was deleted.

0 comments on commit 4c7b61d

Please sign in to comment.