I made a script in python to convert .md files to Scrapbox format

Introduction

Target to be converted

Bullets

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.

スクリーンショット 2020-04-29 21.10.08.png It looks like this on scrapbox

Square brackets []

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

title

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]]

スクリーンショット 2020-04-29 21.11.33.png It looks like this on scrapbox

Formula

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)]

スクリーンショット 2020-04-29 21.12.33.png 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.

Bold

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]]

Source code

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)

How to use

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

I made a script in python to convert .md files to Scrapbox format
I made a script in Python to convert a text file for JSON (for vscode user snippet)
I made a payroll program in Python!
I made a script to display emoji
I made a library to easily read config files with Python
I wrote a function to load a Git extension script in Python
I wrote a script to extract a web page link in Python
I wrote a code to convert quaternions to z-y-x Euler angles in Python
Convert FBX files to ASCII <-> BINARY in Python
I made a web application in Python that converts Markdown to HTML
Note: [Python3] Convert datetime to a string in any format you like
I want to create a window in Python
I made a CLI tool to convert images in each directory to PDF
I want to convert a table converted to PDF in Python back to CSV
I made a program to check the size of a file in Python
I made a Caesar cryptographic program in Python.
I made a module in C language to filter images loaded by Python
I made a script to record the active window using win32gui of Python
I want to write in Python! (1) Code format check
How to convert / restore a string with [] in python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I made a prime number generation program in Python
I tried to implement a pseudo pachislot in Python
A memorandum to run a python script in a bat file
I made a code to convert illustration2vec to keras model
I want to work with a robot in python.
Convert / return class object to JSON format in Python
I made a prime number generation program in Python 2
[Python] Created a method to convert radix in 1 second
I made a python library to do rolling rank
I made an action to automatically format python code
I made a python text
[Python] I made a script that automatically cuts and pastes files on a local PC to an external SSD.
I was soberly addicted to calling awscli from a Python 2.7 script registered in crontab
I made a program to convert images into ASCII art with Python and OpenCV
I can't sleep until I build a server !! (Introduction to Python server made in one day)
I tried to implement a one-dimensional cellular automaton in Python
I made a package to filter time series with python
I made a quick feed reader using feedparser in Python
Linux script to convert Markdown files from JupyterLab format to Qiita format
I made a command to generate a table comment in Django
I tried "How to get a method decorated in Python"
I wrote a script to get a popular site in Japan
I wrote a script to combine the divided ts files
I made a puzzle game (like) with Tkinter in Python
I tried to make a stopwatch using tkinter in python
A set of script files that do wordcloud in Python3
I want to make input () a nice complement in python
I made a script to say hello at my Koshien
I made a Line-bot using Python!
I made a CUI-based translation script (2)
Convert markdown to PDF in Python
I made a fortune with Python.
I made a CUI-based translation script
I made a program to collect images in tweets that I liked on twitter with Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
I made a daemon with Python
Determine the date and time format in Python and convert to Unixtime
How to pass arguments to a Python script in SPSS Modeler Batch
Convert files written in python etc. to pdf with syntax highlighting