Skip to content

Commit

Permalink
Merge pull request #118 from s-kostyaev/improve-markdown-to-org-trans…
Browse files Browse the repository at this point in the history
…lation

Improve code blocks translation
  • Loading branch information
s-kostyaev committed May 17, 2024
2 parents aefd87f + 069bedd commit b0349ae
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 34 deletions.
78 changes: 44 additions & 34 deletions ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -368,43 +368,53 @@ Too low value can break generated code by splitting long comment lines."
(fill-region (point-min) (point-max) nil t t))
(buffer-substring-no-properties (point-min) (point-max))))

(defun ellama--replace-first-begin-src (text)
"Replace first begin src in TEXT."
(if (not (string-match-p (rx (literal "#+BEGIN_SRC")) text))
(replace-regexp-in-string "^[[:space:]]*```\\(\\(.\\|\n\\)*\\)" "#+BEGIN_SRC\\1" text)
text))

(defun ellama--translate-markdown-to-org-filter (text)
"Filter to translate code blocks from markdown syntax to org syntax in TEXT.
This filter contains only subset of markdown syntax to be good enough."
(thread-last text
;; code blocks
(replace-regexp-in-string "^[[:space:]]*```\\(.+\\)$" "#+BEGIN_SRC \\1")
(replace-regexp-in-string "^<!-- language: \\(.+\\) -->\n```" "#+BEGIN_SRC \\1")
(replace-regexp-in-string "^[[:space:]]*```$" "#+END_SRC")
;; lists
(replace-regexp-in-string "^\\* " "+ ")
;; bold
(replace-regexp-in-string "\\*\\*\\(.+?\\)\\*\\*" "*\\1*")
(replace-regexp-in-string "__\\(.+?\\)__" "*\\1*")
(replace-regexp-in-string "<b>\\(.+?\\)</b>" "*\\1*")
;; italic
;; (replace-regexp-in-string "_\\(.+?\\)_" "/\\1/") ;; most of the time it breaks code blocks, so disable it
(replace-regexp-in-string "<i>\\(.+?\\)</i>" "/\\1/")
;; inline code
(replace-regexp-in-string "`\\(.+?\\)`" "~\\1~")
;; underlined
(replace-regexp-in-string "<u>\\(.+?\\)</u>" "_\\1_")
;; strikethrough
(replace-regexp-in-string "~~\\(.+?\\)~~" "+\\1+")
(replace-regexp-in-string "<s>\\(.+?\\)</s>" "+\\1+")
;; headings
(replace-regexp-in-string "^# " "* ")
(replace-regexp-in-string "^## " "** ")
(replace-regexp-in-string "^### " "*** ")
(replace-regexp-in-string "^#### " "**** ")
(replace-regexp-in-string "^##### " "***** ")
(replace-regexp-in-string "^###### " "***** ")
;; badges
(replace-regexp-in-string "\\[\\!\\[.*?\\](\\(.*?\\))\\](\\(.*?\\))" "[[\\2][file:\\1]]")
;;links
(replace-regexp-in-string "\\[\\(.*?\\)\\](\\(.*?\\))" "[[\\2][\\1]]")
;; filling long lines
(ellama--fill-long-lines)))
(thread-last
text
;; code blocks
(replace-regexp-in-string "^[[:space:]]*```\\(.+\\)$" "#+BEGIN_SRC \\1")
(ellama--replace-first-begin-src)
(replace-regexp-in-string "^<!-- language: \\(.+\\) -->\n```" "#+BEGIN_SRC \\1")
(replace-regexp-in-string "^[[:space:]]*```$" "#+END_SRC")
(replace-regexp-in-string "^[[:space:]]*```" "#+END_SRC\n")
(replace-regexp-in-string "```" "\n#+END_SRC\n")
;; lists
(replace-regexp-in-string "^\\* " "+ ")
;; bold
(replace-regexp-in-string "\\*\\*\\(.+?\\)\\*\\*" "*\\1*")
(replace-regexp-in-string "__\\(.+?\\)__" "*\\1*")
(replace-regexp-in-string "<b>\\(.+?\\)</b>" "*\\1*")
;; italic
;; (replace-regexp-in-string "_\\(.+?\\)_" "/\\1/") ;; most of the time it breaks code blocks, so disable it
(replace-regexp-in-string "<i>\\(.+?\\)</i>" "/\\1/")
;; inline code
(replace-regexp-in-string "`\\(.+?\\)`" "~\\1~")
;; underlined
(replace-regexp-in-string "<u>\\(.+?\\)</u>" "_\\1_")
;; strikethrough
(replace-regexp-in-string "~~\\(.+?\\)~~" "+\\1+")
(replace-regexp-in-string "<s>\\(.+?\\)</s>" "+\\1+")
;; headings
(replace-regexp-in-string "^# " "* ")
(replace-regexp-in-string "^## " "** ")
(replace-regexp-in-string "^### " "*** ")
(replace-regexp-in-string "^#### " "**** ")
(replace-regexp-in-string "^##### " "***** ")
(replace-regexp-in-string "^###### " "***** ")
;; badges
(replace-regexp-in-string "\\[\\!\\[.*?\\](\\(.*?\\))\\](\\(.*?\\))" "[[\\2][file:\\1]]")
;;links
(replace-regexp-in-string "\\[\\(.*?\\)\\](\\(.*?\\))" "[[\\2][\\1]]")
;; filling long lines
(ellama--fill-long-lines)))

(defcustom ellama-enable-keymap t
"Enable or disable Ellama keymap."
Expand Down
70 changes: 70 additions & 0 deletions tests/test-ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,76 @@
(let ((element (ellama-context-element-text :content "123")))
(should (string-match "123" (ellama-context-element-extract element)))))

(ert-deftest test-ellama-md-to-org-code-simple ()
(let ((result (ellama--translate-markdown-to-org-filter "Here is your TikZ code for a blue rectangle:
```tex
\\documentclass{article}
\\usepackage{tikz} \\begin{document}
\\begin{tikzpicture} \\node[rectangle, draw=blue, fill=blue!20] (mynode) {Text};
\\end{tikzpicture}
\\end{document}
```
This code will create a rectangle with a blue border and light
blue filling. You can replace \'Text\' with your desired text or other TikZ
elements.")))
(should (string-equal result "Here is your TikZ code for a blue rectangle:
#+BEGIN_SRC tex
\\documentclass{article}
\\usepackage{tikz} \\begin{document}
\\begin{tikzpicture} \\node[rectangle, draw=blue, fill=blue!20] (mynode) {Text};
\\end{tikzpicture}
\\end{document}
#+END_SRC
This code will create a rectangle with a blue border and light
blue filling. You can replace \'Text\' with your desired text or other TikZ
elements."))))

(ert-deftest test-ellama-md-to-org-code-hard ()
(let ((result (ellama--translate-markdown-to-org-filter "Here is your TikZ code for a blue rectangle:
```
\\documentclass{article}
\\usepackage{tikz} \\begin{document}
\\begin{tikzpicture} \\node[rectangle, draw=blue, fill=blue!20] (mynode) {Text};
\\end{tikzpicture}
\\end{document}
```
This code will create a rectangle with a blue border and light
blue filling. You can replace \'Text\' with your desired text or other TikZ
elements.")))
(should (string-equal result "Here is your TikZ code for a blue rectangle:
#+BEGIN_SRC
\\documentclass{article}
\\usepackage{tikz} \\begin{document}
\\begin{tikzpicture} \\node[rectangle, draw=blue, fill=blue!20] (mynode) {Text};
\\end{tikzpicture}
\\end{document}
#+END_SRC
This code will create a rectangle with a blue border and light
blue filling. You can replace \'Text\' with your desired text or other TikZ
elements."))))

(ert-deftest test-ellama-md-to-org-code-nightmare ()
(let ((result (ellama--translate-markdown-to-org-filter "Here is your TikZ code for a blue rectangle:
```
\\documentclass{article}
\\usepackage{tikz} \\begin{document}
\\begin{tikzpicture} \\node[rectangle, draw=blue, fill=blue!20] (mynode) {Text};
\\end{tikzpicture}
\\end{document}```This code will create a rectangle with a blue border and light
blue filling. You can replace \'Text\' with your desired text or other TikZ
elements.")))
(should (string-equal result "Here is your TikZ code for a blue rectangle:
#+BEGIN_SRC
\\documentclass{article}
\\usepackage{tikz} \\begin{document}
\\begin{tikzpicture} \\node[rectangle, draw=blue, fill=blue!20] (mynode) {Text};
\\end{tikzpicture}
\\end{document}
#+END_SRC
This code will create a rectangle with a blue border and light
blue filling. You can replace \'Text\' with your desired text or other TikZ
elements."))))

(provide 'test-ellama)

;;; test-ellama.el ends here

0 comments on commit b0349ae

Please sign in to comment.