Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YAML front-matter updating #184

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions lib/octopress/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def path
def default_template
'post'
end

def unpublish
@options['date'] = read_post_yaml('date')
@options['title'] = read_post_yaml('title')
Expand All @@ -51,7 +51,7 @@ def unpublish
}

Draft.new(site, post_options).write

# Remove the old post file
#
FileUtils.rm @options['path']
Expand All @@ -67,27 +67,53 @@ def read
end
end

# Extracts the YAML front matter
#
def yaml_front_matter
parse_content unless @yaml_front_matter
@yaml_front_matter
end

# Extracts the body of the post after the YAML front matter
#
def body
parse_content unless @body
@body
end

# Get title from post file
#
def read_post_yaml(key)
match = read.match(/#{key}:\s*(.+)?$/)
match = yaml_front_matter.match(/^\s*#{key}:\s*(.+)?$/)
match[1] if match
end


FRONT_MATTER_REGEXP = /\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*\n?$)/m

def parse_content
match = read.match(FRONT_MATTER_REGEXP)
if match
@yaml_front_matter = match[0]
@body = match.post_match
else
@yaml_front_matter = ""
@body = read
end
end

# Get content from draft post file
# also update the draft's date configuration
#
def read_post_content
if @options['date']
# remove date if it exists
content = read.sub(/date:.*$\n/, "")

# Insert date after title
content.sub(/(title:.+$)/i, '\1'+"\ndate: #{@options['date']}")
# update date if it exists
front_matter = yaml_front_matter
.sub(/date:.*$\n/, "")
.sub(/(title:.+$)/i, '\1'+"\ndate: #{@options['date']}")
"#{front_matter}#{body}"
else
read
end
end

end
end