# Converts Wordpress .html to Markdown .md file |
# usage: |
# ruby ./wordpress-html-to-md.rb '_posts/post.html' |
# ruby ./wordpress-html-to-md.rb '_posts' |
# requires: |
# gem install reverse_markdown |
require'fileutils' |
require'reverse_markdown' |
# default config |
source='_posts' |
ifARGV.length > 0 |
source=ARGV[0] |
end |
# define functions |
defopen_file_content(file) |
puts'Open '#{file}' |
fh=openfile |
content=fh.read |
fh.close |
returncontent |
end |
# usage: `puts convert_html_to_markdown('be bold')` |
defconvert_html_to_markdown(html,default_frontmatter=') |
# check for Front Matter markdown in HTML |
pattern=/(---.*---)(.*)/m |
htmlcontent=html |
frontmatter=default_frontmatter |
matches=html.match(pattern) |
ifmatches && matches.length3 |
puts'Front matter detected...' |
frontmatter=matches[1] |
htmlcontent=matches[2] |
#puts '#{frontmatter}n#{htmlcontent}' |
end |
puts'Convert HTML to markdown...' |
markdown=ReverseMarkdown.convert(htmlcontent,github_flavored: true) |
puts'Converted HTML to markdown' |
# puts markdown.inspect |
iffrontmatter.length > 1 |
return'#{frontmatter}n#{markdown}' |
else |
returnmarkdown |
end |
end |
defsave_file_content(sourceFile,content,ext='md',deleteSourceFile=false) |
destFile='#{File.dirname(sourceFile)}/#{File.basename(sourceFile,'.*')}.#{ext}' |
puts'Write to file: '#{destFile}' |
File.write(destFile,content) |
ifdeleteSourceFile |
FileUtils.remove_file(sourceFile) |
end |
end |
defconvert_html_file_to_md(sourceFile) |
# read file |
content=open_file_content(sourceFile) |
# convert content to markdown |
markdown=convert_html_to_markdown(content,'---nlayout: postn---') |
# save file as md. Note: change the deleteSourceFile arg to true to delete the html source file |
save_file_content(sourceFile,markdown,'md',false) |
end |
# script start |
ifFile.directory?(source) |
sourceFilter='#{source}**/*.html' |
# process all html files in directory |
i=0 |
Dir.glob(sourceFilter)do |f| |
convert_html_file_to_md(f) |
i += 1 |
end |
puts'#{i} html files converted.' |
elsifFile.file?(source) |
convert_html_file_to_md(source) |
else |
puts'File not found '#{source}' |
end |
- Hashes for html2md-0.1.7-py3-none-any.whl; Algorithm Hash digest; SHA256: 6b3542121ade2e99b1c9fe07a2a719a93f5f0b7ea8cf16ee5dace9: Copy MD5.
- HTML to MD Converter. This VSCode extension converts HTML data to Markdown table format.
- ❓ How can I convert HTML to MD? First you need to add file for conversion: drag and drop your HTML file or click the 'Choose File' button. Then click the 'Convert' button. When HTML to MD conversion is completed, you can download your MD file.
Convert Md To Html
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Convert Word To Markdown
HTML to MD Converter CloudConvert is an online document converter. Amongst many others, we support PDF, DOCX, PPTX, XLSX. Thanks to our advanced conversion technology the quality of the output will be exactly the same as if the file was saved through the latest Microsoft Office 2019 suite.