forked from kenkeiras/org-rw
Add simple link modification mechanism.
This commit is contained in:
parent
d6133a1e9b
commit
cb86d88006
@ -4,7 +4,7 @@ import logging
|
|||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import List, Tuple
|
from typing import List, Tuple, Union
|
||||||
|
|
||||||
BASE_ENVIRONMENT = {
|
BASE_ENVIRONMENT = {
|
||||||
"org-footnote-section": "Footnotes",
|
"org-footnote-section": "Footnotes",
|
||||||
@ -75,22 +75,42 @@ def get_tokens(value):
|
|||||||
raise Exception("Unknown how to get tokens from: {}".format(value))
|
raise Exception("Unknown how to get tokens from: {}".format(value))
|
||||||
|
|
||||||
|
|
||||||
|
class RangeInRaw:
|
||||||
|
def __init__(self, content, start, end):
|
||||||
|
self._content = content
|
||||||
|
self._start = start
|
||||||
|
self._end = end
|
||||||
|
|
||||||
|
def update_range(self, contents):
|
||||||
|
if len(contents) == (self._end) - self._start:
|
||||||
|
for i in range(self._start, self._end):
|
||||||
|
self._content.contents[i] = contents[i - self._start]
|
||||||
|
else:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
def get_links_from_content(content):
|
def get_links_from_content(content):
|
||||||
in_link = False
|
in_link = False
|
||||||
in_description = False
|
in_description = False
|
||||||
link_value = []
|
link_value = []
|
||||||
link_description = []
|
link_description = []
|
||||||
|
|
||||||
for tok in get_tokens(content):
|
for i, tok in enumerate(get_tokens(content)):
|
||||||
if isinstance(tok, LinkToken):
|
if isinstance(tok, LinkToken):
|
||||||
if tok.tok_type == LinkTokenType.OPEN_LINK:
|
if tok.tok_type == LinkTokenType.OPEN_LINK:
|
||||||
in_link = True
|
in_link = True
|
||||||
|
open_link_token = i + 1
|
||||||
elif tok.tok_type == LinkTokenType.OPEN_DESCRIPTION:
|
elif tok.tok_type == LinkTokenType.OPEN_DESCRIPTION:
|
||||||
in_description = True
|
in_description = True
|
||||||
elif tok.tok_type == LinkTokenType.CLOSE:
|
elif tok.tok_type == LinkTokenType.CLOSE:
|
||||||
|
rng = RangeInRaw(content, open_link_token, i)
|
||||||
|
yield Link(
|
||||||
|
"".join(link_value),
|
||||||
|
"".join(link_description) if in_description else None,
|
||||||
|
rng,
|
||||||
|
)
|
||||||
in_link = False
|
in_link = False
|
||||||
in_description = False
|
in_description = False
|
||||||
yield Link("".join(link_value), "".join(link_description))
|
|
||||||
link_value = []
|
link_value = []
|
||||||
link_description = []
|
link_description = []
|
||||||
elif isinstance(tok, str) and in_link:
|
elif isinstance(tok, str) and in_link:
|
||||||
@ -399,9 +419,10 @@ class Line:
|
|||||||
|
|
||||||
|
|
||||||
class Link:
|
class Link:
|
||||||
def __init__(self, value, description):
|
def __init__(self, value: str, description: str, origin: RangeInRaw):
|
||||||
self.value = value
|
self._value = value
|
||||||
self.description = description
|
self._description = description
|
||||||
|
self._origin = origin
|
||||||
|
|
||||||
def get_raw(self):
|
def get_raw(self):
|
||||||
if self.description:
|
if self.description:
|
||||||
@ -409,6 +430,32 @@ class Link:
|
|||||||
else:
|
else:
|
||||||
return "[[{}]]".format(self.value)
|
return "[[{}]]".format(self.value)
|
||||||
|
|
||||||
|
def _update_content(self):
|
||||||
|
new_contents = []
|
||||||
|
new_contents.append(self._value)
|
||||||
|
if self._description:
|
||||||
|
new_contents.append(LinkToken(LinkTokenType.OPEN_DESCRIPTION))
|
||||||
|
new_contents.append(self._description)
|
||||||
|
self._origin.update_range(new_contents)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def value(self):
|
||||||
|
return self._value
|
||||||
|
|
||||||
|
@value.setter
|
||||||
|
def value(self, new_value):
|
||||||
|
self._value = new_value
|
||||||
|
self._update_content()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def description(self):
|
||||||
|
return self._description
|
||||||
|
|
||||||
|
@description.setter
|
||||||
|
def description(self, new_description):
|
||||||
|
self._description = new_description
|
||||||
|
self._update_content()
|
||||||
|
|
||||||
|
|
||||||
class Text:
|
class Text:
|
||||||
def __init__(self, contents, line):
|
def __init__(self, contents, line):
|
||||||
|
Loading…
Reference in New Issue
Block a user