emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/ellama b0349ae3cb 2/3: Merge pull request #118 from s-k


From: ELPA Syncer
Subject: [elpa] externals/ellama b0349ae3cb 2/3: Merge pull request #118 from s-kostyaev/improve-markdown-to-org-translation
Date: Fri, 17 May 2024 12:57:56 -0400 (EDT)

branch: externals/ellama
commit b0349ae3cb11279265f65c88839f2dfff451444f
Merge: aefd87f22a 069bedd596
Author: Sergey Kostyaev <s-kostyaev@users.noreply.github.com>
Commit: GitHub <noreply@github.com>

    Merge pull request #118 from s-kostyaev/improve-markdown-to-org-translation
    
    Improve code blocks translation
---
 ellama.el            | 78 +++++++++++++++++++++++++++++-----------------------
 tests/test-ellama.el | 70 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 114 insertions(+), 34 deletions(-)

diff --git a/ellama.el b/ellama.el
index ef8745518e..3e638b5fc5 100644
--- a/ellama.el
+++ b/ellama.el
@@ -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."
diff --git a/tests/test-ellama.el b/tests/test-ellama.el
index 67ad3e5337..7c7ffc03ee 100644
--- a/tests/test-ellama.el
+++ b/tests/test-ellama.el
@@ -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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]