Skip to content

Commit

Permalink
First commits (working)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvquanghuy committed Oct 27, 2020
0 parents commit 80e8d0d
Show file tree
Hide file tree
Showing 20 changed files with 945 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea
config.json
gen_docs_final/*
gen_docs_raw/*
gen_images/*
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
readme-converter
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.5.1
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'
gem 'faraday'
gem 'pry'
gem 'rspec'
gem 'kramdown'
42 changes: 42 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.3)
diff-lcs (1.4.4)
faraday (1.1.0)
multipart-post (>= 1.2, < 3)
ruby2_keywords
kramdown (2.3.0)
rexml
method_source (1.0.0)
multipart-post (2.1.1)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
rexml (3.2.4)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.3)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-mocks (3.9.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.4)
ruby2_keywords (0.0.2)

PLATFORMS
ruby

DEPENDENCIES
faraday
kramdown
pry
rspec

BUNDLED WITH
1.17.2
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Export Script for Readme.com

This little script exports data from a readme.com (readme.io) documentation site into respective markdown.

What it does:

* Export pages into markdown
* Download images from readme.com

What it doesn't do yet:

* Get list of pages in your site
* Get the navigation structure
4 changes: 4 additions & 0 deletions config.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"README_APIKEY": "your apikey here",
"IMAGE_PATH_PREFIX": "https://cdn.holistics.io/docs/readme/"
}
23 changes: 23 additions & 0 deletions lib/api_header_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

class ApiHeaderBlockReplacer
attr_accessor :block_string

def initialize(block_string)
self.block_string = block_string
end

def get_markdown()
json_string = block_string.strip[18..-9]
h = JSON.parse(json_string)

"# #{h['title']}"
end

def self.scan(body)
body.gsub /\[block:api-header\](.+?)\[\/block\]/m do |str|
puts "Found API Header block ..."
ApiHeaderBlockReplacer.new(str).get_markdown
end
end
end

30 changes: 30 additions & 0 deletions lib/callout_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

class CalloutBlockReplacer
attr_accessor :block_string

def initialize(block_string)
self.block_string = block_string
end

def get_markdown()
json_string = block_string.strip[15..-9]
h = JSON.parse(json_string)
type = h['type']
title = (h['title'] ? " #{h['title']}" : nil)
body = h['body']

<<~MARKDOWN
:::#{type}#{title}
#{body}
:::
MARKDOWN
end

def self.scan(body)
body.gsub /\[block:callout\](.+?)\[\/block\]/m do |str|
puts "Found Callout block ..."
CalloutBlockReplacer.new(str).get_markdown
end
end
end

34 changes: 34 additions & 0 deletions lib/code_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

class CodeBlockReplacer
attr_accessor :block_string

def initialize(block_string)
self.block_string = block_string
end

def get_markdown()
json_string = block_string.strip[12..-9]
h = JSON.parse(json_string)

markdowns = h['codes'].map do |item|
code = item['code']
language = item['language']
puts " Language: #{language}"

<<-MARKDOWN
```#{language}
#{code}
```
MARKDOWN
end
markdowns.join("\n")
end

def self.scan(body)
body.gsub /\[block:code\](.+?)\[\/block\]/m do |str|
puts "Found code block ..."
CodeBlockReplacer.new(str).get_markdown
end
end
end

25 changes: 25 additions & 0 deletions lib/embed_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

class EmbedBlockReplacer
attr_accessor :block_string

def initialize(block_string)
self.block_string = block_string
end

def get_markdown()
json_string = block_string.strip[13..-9]
h = JSON.parse(json_string)

<<~HTML
<iframe src="#{h['url']}"></iframe>
HTML
end

def self.scan(body)
body.gsub /\[block:embed\](.+?)\[\/block\]/m do |str|
puts "Found embed block ..."
EmbedBlockReplacer.new(str).get_markdown
end
end
end

22 changes: 22 additions & 0 deletions lib/html_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

class HtmlBlockReplacer
attr_accessor :block_string

def initialize(block_string)
self.block_string = block_string
end

def get_markdown()
json_string = block_string.strip[12..-9]
h = JSON.parse(json_string)
h['html']
end

def self.scan(body)
body.gsub /\[block:html\](.+?)\[\/block\]/m do |str|
puts "Found HTML block ..."
HtmlBlockReplacer.new(str).get_markdown
end
end
end

49 changes: 49 additions & 0 deletions lib/image_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

class ImageBlockReplacer
attr_accessor :readme_block, :image_path_prefix

def download_image(url)
uri = URI.parse(url)
filename = File.basename(uri.path)

r = Faraday.get(url)
if r.status != 200
puts "Error: #{response.status} - #{JSON.parse(response.body)['message']}"
return
end

dest_filepath = "./gen_images/#{filename}"
File.open(dest_filepath, 'wb') {|f| f.write(r.body)}
filename
end

def initialize(readme_block, image_path_prefix: nil)
self.readme_block = readme_block
self.image_path_prefix = image_path_prefix
end

def get_markdown()
h = JSON.parse(readme_block.strip[13..-9])

markdowns = h['images'].map do |image|
url = image['image'].first
return '' if url.nil?

puts "Downloading image #{url} ..."
filename = download_image(url)

cdn_filepath = "#{self.image_path_prefix}#{filename}"

"\n![](#{cdn_filepath})\n"
end
markdowns.join("\n")
end

def self.scan(body, image_path_prefix: '/images/')
body.gsub /\[block:image\](.+?)\[\/block\]/m do |str|
puts "Found image block ..."
ImageBlockReplacer.new(str, image_path_prefix: image_path_prefix).get_markdown
end
end
end

13 changes: 13 additions & 0 deletions lib/internal_href_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

class InternalHrefReplacer

def self.scan(body)
body.gsub /\(doc:([^)]+)\)/ do
v = Regexp.last_match[1]
parts = v.split('#')
anchor = (parts[1].nil? ? '' : "##{parts[1]}")
"(" + parts[0] + ".md" + anchor + ")"
end
end
end

58 changes: 58 additions & 0 deletions lib/parameters_block_replacer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'kramdown'

class ParametersBlockReplacer
attr_accessor :block_string

def initialize(block_string)
self.block_string = block_string
end

def get_markdown()
json_string = block_string.strip[18..-9]
h = JSON.parse(json_string)
cols = h['cols'].to_i
rows = h['rows'].to_i
data = h['data']

headers = (0..cols-1).to_a.map do |i|
data["h-#{i}"]
end
records = (0 .. rows-1).to_a.map do |i|
(0..cols-1).to_a.map do |j|
data["#{i}-#{j}"]
end
end

headers_html = headers.map do |item|
"<th>#{item}</th>"
end.join("\n")

records_html = records.map do |row|
"<tr>" +
row.map do |cell|
html = Kramdown::Document.new(cell.to_s).to_html
"<td>#{html}</td>"
end.join("\n") +
"</tr>"
end.join("\n")

<<~HTML
<table>
<thead>
#{headers_html}
</thead>
<tbody>
#{records_html}
</tbody>
</table>
HTML
end

def self.scan(body)
body.gsub /\[block:parameters\](.+?)\[\/block\]/m do |str|
puts "Found parameters block ..."
ParametersBlockReplacer.new(str).get_markdown
end
end
end

Loading

0 comments on commit 80e8d0d

Please sign in to comment.