Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
マークダウンパーサーの自作
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
suzakutakumi
December 11, 2021
Technology
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
マークダウンパーサーの自作
suzakutakumi
December 11, 2021
Transcript
22 slides
マークダウンパーサー の自作 2021/12/11 Zli 大LT
自己紹介 HN:朱雀 匠(本名:鈴木 拓眞) Twitter: @suzakutakumi3 Portfolio: http://suzakutakumi.mydns.jp/
None
None
マークダウンパーサーとは マークダウンをHTML等に変換してくれる奴です。
マークダウンを使うところ - Notion - Qiita - GitHub - esa.io -
VSCode
使ってて思ったこと - 色変えたい - 改行がスペース2個書かないとされないの意味わからん - 文字サイズも変更したい どうしたら良いのか?
自分で作る!
注意点! 構文解析とか文法解析とかは知らない 行きあたりばったりの何となくで作成しています
見出しの実装 if l>0 and row[0]=='#': cnt=1 while row[cnt]=='#': cnt+=1 if
row[cnt]==" ": row="<h%d>"%cnt+row[cnt+1:-4]+"</h%d>"%cnt これは1行:1行の対応なので、簡単に実装できました。
リンクの実装 ans=re.search(r"\[.+\]\(.+\)",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second=row[sl[0]:sl[1]] last=row[sl[1]:] content=re.search(r"\[.+\]",second).span() link=re.search(r"\(.+\)",second).span()
row=first+'<a href="%s">%s</a>'%(second[link[0]+1:link[1]-1],second[content[0]+1:content[1]-1])+last こちらも1行:1行の対応なので、簡単に実装できました。 正規表現で、その行にリンクがあるか?リンクの位置などを抽出しています。
画像の実装 ans=re.search(r"!\[.*\]\(.+\)",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second=row[sl[0]:sl[1]] last=row[sl[1]:] content=re.search(r"\[.+\]",second).span() link=re.search(r"\(.+\)",second).span()
row=first+'<img src="%s" alt="%s"/>'%(second[link[0]+1:link[1]-1],second[content[0]+1:content[1]-1])+last ほぼ、リンクと変わらないですね
リストの実装1 リストとは
リストの実装2 コードは省略 こちらは一番時間がかかっています。 リストのネストに対応しています。
サイズ変更の実装 ans=re.search(r"<[0-9]+>",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second='<font size="%s">'%row[sl[0]+1:sl[1]-1] last=row[sl[1]:] row=first+second+last
fin_list.append("</font>") こちらも、正規表現を用いて、サイズの実装をしました。
色の実装 ans=re.search(r"<#[0-9a-fA-F]+>",row) if ans!=None: sl=ans.span() first=row[:sl[0]] second='<font color="%s">'%row[sl[0]+1:sl[1]-1] last=row[sl[1]:] row=first+second+last
fin_list.append("</font>") colorTexts=["black","gray","silver","white","blue","navy","teal","green","lime","aqua","yellow","red","fuchsia","olive"," purple","maroon"] for texts in colorTexts: ans=re.search("<%s>"%texts,row) if ans!=None: sl=ans.span() first=row[:sl[0]] second='<font color="%s">'%row[sl[0]+1:sl[1]-1] last=row[sl[1]:] row=first+second+last fin_list.append("</font>") こちらも正規表現を利用して、実装しています。
デモ
Webサーバでの実装 将来的には、自分のポートフォリオサイトで使いたいので、Webで編 集できるようにしたい!
Flaskで実装 from flask import Flask,render_template,request import sys sys.path.append( '../') from
command import MarkdownParser as MP app = Flask(__name__) @app.route('/') def Index(): return render_template( "index.html") @app.route('/test',methods=['GET','POST']) def Test(): data=request.get_data().decode() ans=MP.main(data.split( '\n')) return '\n'.join(ans) if __name__ == "__main__": app.run(debug=True) /testにアクセスすると、bodyの内容を 先ほど作ったマークダウンパーサーを呼び出し、変換しています。
デモ
今後について • シンタックスハイライトの対応やその他の便利機能を追加する • 構文解析などを習ったので、もう少ししっかりした設計にしたい • CSSをしっかりさせる • 自分のポートフォリオ用に適応させる
最後に 下のリンクからプログラムがおいてあるリポジトリを見ることができます。 https://github.com/suzakutakumi/MarkdownParser
More Decks by suzakutakumi
See All by suzakutakumi
ピクロス作成の中間発表
suzakutakumi
0
120
しゅみろん
suzakutakumi
0
200
trap-search
suzakutakumi
1
72
Pyramid Makerの作成
suzakutakumi
0
73
絵文字ジェネレータボットの作成
suzakutakumi
0
180
send_discord
suzakutakumi
0
75
独自ドメインについて
suzakutakumi
0
71
ESP32とAlexaを用いたエアコン制御
suzakutakumi
0
1.4k
スターリンマージソート
suzakutakumi
3
660
Other Decks in Technology
See All in Technology
属人化したISMS運用をGitHubとAIで仕組み化した1年間の記録
sioncojp
4
2.2k
ゼロから作るWebアプリの新定番!?Amplify Gen 2 × AWS Blocksで実現する「開発速度とRDBの柔軟性のいいとこ取り」
amixedcolor
1
240
Kiro IDE 入門 (画面の各部から、頼み方、任せ方、戻し方まで) | 目指せ Kiro マスター 02
inariku
PRO
4
210
AWSを利用する上で知っておきたい名前解決の話 2026
nagisa53
5
3.4k
あの日、見つかっていなかったらどうなっていたか
kazzpapa3
2
110
思考過程から秘密が漏れる:Amazon Bedrock上のKimi K3を5つの方法で攻めたら、25回中22回漏洩した話
yama3133
1
120
AIエージェントの監視、思考を見るか行動を見るか / Monitoring AI Agents: Thoughts or Actions
yusukeshimizu
2
180
Context != Context: One window, many lifecycles (AI Native DevCon, Nov 2026)
birgitta410
0
100
TSKaigi Sendai 2026 - tsgo時代のTypeScript Playgroundはどう動くか
teamlab
PRO
0
120
Sizing, Benchmarking and Performance Tuning Apache Flink Clusters
rmetzger
0
150
Claude Codeで作ったアプリを安全に公開する/Built with Claude Japan 2026
yoshidashingo
0
230
Cloudflareの向き先
chimame
1
290
Featured
See All Featured
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Building Adaptive Systems
keathley
44
3.3k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Why Our Code Smells
bkeepers
PRO
340
58k
The Cult of Friendly URLs
andyhume
79
7.1k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
560
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
2
440
How to build a perfect <img>
jonoalderson
1
6.2k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
620
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.7k