Skip to content

Commit

Permalink
refs #3 notation: links
Browse files Browse the repository at this point in the history
  • Loading branch information
shimizukawa committed Aug 14, 2019
1 parent bf4f891 commit 4236d15
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/3.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notation: links
28 changes: 21 additions & 7 deletions scrap2rst/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def get_api_url(url: str) -> str:
return api_url


def get_user_url(url: str) -> str:
parts = urlparse(url)
path = quote(parts.path)
if path.startswith('/api/'):
user = path.strip('/').split('/')[2]
else:
user = path.strip('/').split('/')[0]
user_url = urlunparse((*parts[:2], user, '', '', ''))
return user_url


def fetch(api_url: str) -> str:
logger.info('fetch: %s', api_url)
data = urlopen(api_url).read()
Expand Down Expand Up @@ -53,8 +64,9 @@ def wlen(text: str) -> int:


class Convert:
def __init__(self, data: str):
def __init__(self, data: str, user_url: str):
self.data = data
self.user_url = user_url
self.line_states = []
self.link_targets = {}

Expand Down Expand Up @@ -124,12 +136,12 @@ def parse_inline_and_render(self, line: str, ln: int) -> str:
m = M['link'](line)
_pre, _target, _post = m.groups()
if M['link_external'](_target):
_link, _title = M['link_external'](_target)
result = '{0} `{2} <{1}>`__ {3}'.format(_pre, _link, _title, _post)
_link, _title = M['link_external'](_target).groups()
else:
_link = _target.replace(' ', '_')
result = '{0} `{1}`_ {2}'.format(_pre, _target, _post)
self.link_targets[_target] = f'https://scrapbox.io/shimizukawa/{_link}'
_link = self.user_url + '/' + _target.replace(' ', '_')
_title = _target
result = '{0} `{1}`_ {2}'.format(_pre, _title, _post)
self.link_targets[_title] = _link
else:
name = 'NOTHING'
result = line
Expand All @@ -153,5 +165,7 @@ def run(self) -> str:

def convert(url: str) -> str:
api_url = get_api_url(url)
user_url = get_user_url(url)
logger.info('user url: %s', user_url)
data = fetch(api_url)
return Convert(data).run()
return Convert(data, user_url).run()

0 comments on commit 4236d15

Please sign in to comment.