input.md
*Image captioning is the task of giving a natural language description to the visual scene.
*Neural encoder in this field in the last 5 years-decoder is popular
*sota is a CNN encoder, LSTM(And Transformer)It consists of a decoder and an attention mechanism.
In markdown, write with *
or -
.
In scrapbox, one space character is equivalent to one indent + bullet (black circle ●), so it will be replaced with a space.
output.txt
Image captioning is the task of giving a natural language description to the visual scene.
Neural encoder in this field in the last 5 years-decoder is popular
sota is a CNN encoder, LSTM(And Transformer)It consists of a decoder and an attention mechanism.
It looks like this on scrapbox
input.md
[27]Is a CNN encoder,Proposing a model consisting of LSTM decoder
Converts the square brackets [] used to cite a paper to parentheses () The reason for this is that in scrapbox the square brackets [] mean links between articles.
output.txt
(27)Is a CNN encoder,Proposing a model consisting of LSTM decoder
input.md
## 2.Related research
### 2.1. Image Captioning
In Markdown, h1 to h6 are expressed as headings by continuing multiple #
.
There are no headings in scrapbox, so use bold instead. Enclose bold letters in square brackets []. [[String to emphasize]]
output.txt
[[ 2.Related research]]
[[ 2.1. Image Captioning]]
It looks like this on scrapbox
input.md
$f_t = \sigma(W_f \cdot [h_{t-1}, x_t] + b_f)$
In Markdown, you can express a formula in one line with $ formula $
.
With scrapbox you can do the same with [$ formula]
.
output.txt
[$ f_t = \sigma(W_f \cdot (h_{t-1}, x_t) + b_f)]
It looks like this on scrapbox
By the way, in scrapbox, if you enter a formula number such as \ tag {1}
, it will not fit in one line, but will be two lines. This time, we cannot handle this.
input.md
**Emphasis test**
In Markdown, you can express it in bold by using a method such as ** the character you want to emphasize **
.
In scrapbox, as mentioned above, it becomes [[character string to be emphasized]]
.
output.txt
[[Emphasis test]]
Just honestly replace the string with re
andstr.replace ()
.
I would appreciate it if you could let me know if there is something strange.
The syntax highlighting color has disappeared due to the effect of escape \
...
md2scrapbox.py
import os
import argparse
import re
parser = argparse.ArgumentParser()
parser.add_argument("in_file")
args = parser.parse_args()
sentence = "" # output
tab = " "
space = " "
bullet = "* "
strong = r"\*\*.*\*\*.?"
title = r"#*"
math = r"\$[^\$]*\$"
with open(args.in_file, "r") as f:
lines = f.readlines()
for line in lines:
# blaket
line = line.replace("[", "(")
line = line.replace("]", ")")
# indentation
for m in re.finditer(tab, line):
line = re.sub(tab, space, line)
line = line.replace(bullet, space, 1)
# title
m = re.search(title, line)
if len(m.group()) > 0:
line = "[[" + line[m.end():].replace("\n", "") + "]]\n"
# math
m = re.findall(math, line)
if m:
for matched in m:
m2 = re.sub(r"^\$", "[$ ", matched)
m2 = re.sub(r"\$$", "]", m2)
line = line.replace(matched, m2)
sentence += line
continue
else:
# enphasis
m = re.findall(strong, line)
if m:
for matched in m:
m2 = re.sub(r"^\*\*", "[[", matched)
m2 = re.sub(r"\*\*$", "]]", m2)
line = line.replace(matched, m2)
sentence += line
print("==========")
print(sentence)
dirname = os.path.dirname(args.in_file)
name = os.path.basename(args.in_file)
name, ext = os.path.splitext(name)
name = name + "_scrapbox.txt"
name = os.path.join(dirname, name)
print(name)
with open(name, "w") as f:
f.write(sentence)
python md2scrapbox.py path/to/some_file.md
The conversion result will be renamed and saved in the same location as the input
In the above example, it will be path / to / some_file_scrapbox.txt
.
All you have to do is paste the completed string into scrapbox.
Recommended Posts