Skip to content

Commit

Permalink
feat: add support to page languages
Browse files Browse the repository at this point in the history
  • Loading branch information
dvinciguerra committed Mar 6, 2024
1 parent 165b0b3 commit b8a5766
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@

## [Unreleased]


## [0.2.0] - 2024-03-06

- Added support to page languages

## [0.1.0] - 2023-05-18

- Initial release


29 changes: 24 additions & 5 deletions lib/tldr/cli/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ class Commands
# desc 'list all entries in the local database'
# end

option :lang do
optional
short '-l'
long '--lang=string'
default 'en'
permit %w[ar bn bs ca cs da de en es fa fi fr hi id it ja ko lo ml ne nl no pl pt_BR pt_PT ro ru sh sr sv ta th
tr uk uz zh zh_TW]
desc 'select language of page to be displayed (default: en)'
end

option :platform do
optional
short '-p'
Expand All @@ -86,22 +96,26 @@ def run
elsif params[:version]
version
elsif params[:query]
query = params[:query]
platform = params[:platform]
query, lang, platform =
params.to_h.values_at(:query, :lang, :platform)

response = Faraday.get("#{URL_BASE}/#{platform}/#{query}#{URL_SUFFIX}")
page_path = "/#{platform}/#{query}"

response = Faraday.get(remote_path(page_path, lang: lang))
return not_found unless response.success?

markdown = TTY::Markdown.parse(response.body, symbols: { override: { bullet: '-' } })
puts markdown
render_markdown(response.body)
else
print help
end
end

private

def render_markdown(content)
puts TTY::Markdown.parse(content, symbols: { override: { bullet: '-' } })
end

def version
puts <<~MESSAGE
tldr v#{TLDR::CLI::VERSION} (v#{TLDR::CLI::VERSION})
Expand All @@ -116,6 +130,11 @@ def not_found
Submit new pages here: https://github.com/tldr-pages/tldr
MESSAGE
end

def remote_path(fragment, lang: 'en', relative: false)
lang = lang == 'en' ? '' : ".#{lang}"
"#{relative ? '' : URL_BASE}#{lang}#{fragment}#{URL_SUFFIX}"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tldr/cli/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module TLDR
module CLI
VERSION = "0.1.0"
VERSION = '0.2.0'
end
end
103 changes: 103 additions & 0 deletions new.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
lib/tldr/cli/commands.rb | 57 +++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 52 insertions(+), 5 deletions(-)

diff --git a/lib/tldr/cli/commands.rb b/lib/tldr/cli/commands.rb
index c9aea38..8998696 100644
--- a/lib/tldr/cli/commands.rb
+++ b/lib/tldr/cli/commands.rb
@@ -15,6 +15,9 @@ module TLDR
URL_SUFFIX =
ENV.fetch('TLDR_URL_SUFFIX', '.md')

+ LOCAL_BASE =
+ ENV.fetch('TLDR_LOCAL_BASE', "#{Dir.home}/.config/tldr/pages")
+
usage do
program 'tldr'

@@ -63,6 +66,25 @@ module TLDR
# desc 'list all entries in the local database'
# end

+ option :lang do
+ optional
+ short '-l'
+ long '--lang=string'
+ default 'en'
+ permit %w[ar bn bs ca cs da de en es fa fi fr hi id it ja ko lo ml ne nl no pl pt_BR pt_PT ro ru sh sr sv ta th tr uk uz zh zh_TW]
+ desc 'select language of page to be displayed (default: en)'
+ end
+
+ option :source do
+ optional
+ short '-s'
+ long '--source=string'
+ default 'local'
+ permit %w[local remote]
+ desc 'select page source to be local or remote (default: local)'
+ end
+
+
option :platform do
optional
short '-p'
@@ -86,15 +108,22 @@ module TLDR
elsif params[:version]
version
elsif params[:query]
- query = params[:query]
- platform = params[:platform]
+ query, lang, source, platform =
+ params.to_h.values_at(:query, :lang, :source, :platform)
+
+ page_path = "/#{platform}/#{query}"

- response = Faraday.get("#{URL_BASE}/#{platform}/#{query}#{URL_SUFFIX}")
+ if source == 'local' && local_page?(local_path(page_path, lang: lang))
+ content = File.read(local_path(page_path, lang: lang))
+ render_markdown(content)
+ return
+ end

+ puts "Fetching page from #{source} source..." if
+ response = Faraday.get(remote_path(page_path, lang: lang))
return not_found unless response.success?

- markdown = TTY::Markdown.parse(response.body, symbols: { override: { bullet: '-' } })
- puts markdown
+ render_markdown(response.body)
else
print help
end
@@ -102,6 +131,10 @@ module TLDR

private

+ def render_markdown(content)
+ puts TTY::Markdown.parse(content, symbols: { override: { bullet: '-' } })
+ end
+
def version
puts <<~MESSAGE
tldr v#{TLDR::CLI::VERSION} (v#{TLDR::CLI::VERSION})
@@ -116,6 +149,20 @@ module TLDR
Submit new pages here: https://github.com/tldr-pages/tldr
MESSAGE
end
+
+ def local_path(fragment, lang: 'en', relative: false)
+ lang = lang == 'en' ? '' : ".#{lang.to_s}"
+ "#{relative ? '' : LOCAL_BASE}#{lang}#{fragment}#{URL_SUFFIX}"
+ end
+
+ def remote_path(fragment, lang: 'en', relative: false)
+ lang = lang == 'en' ? '' : ".#{lang.to_s}"
+ "#{relative ? '' : URL_BASE}#{lang}#{fragment}#{URL_SUFFIX}"
+ end
+
+ def local_page?(page_path)
+ File.exist?(page_path)
+ end
end
end
end

0 comments on commit b8a5766

Please sign in to comment.