Skip to content

Commit

Permalink
Merge pull request #120 from s-kostyaev/fix-bad-code-blocks
Browse files Browse the repository at this point in the history
Fix bad code blocks during md to org conversion
  • Loading branch information
s-kostyaev authored Jun 2, 2024
2 parents 1e1db6d + 4b053d3 commit 07b4597
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,17 @@ Too low value can break generated code by splitting long comment lines."
(replace-regexp-in-string "^[[:space:]]*```\\(\\(.\\|\n\\)*\\)" "#+BEGIN_SRC\\1" text)
text))

(defun ellama--replace-bad-code-blocks (text)
"Replace code src blocks in TEXT."
(with-temp-buffer
(insert text)
(goto-char (point-min))
;; skip good code blocks
(while (re-search-forward "#\\+BEGIN_SRC\\(.\\|\n\\)*?#\\+END_SRC" nil t))
(while (re-search-forward "#\\+END_SRC\\(\\(.\\|\n\\)*?\\)#\\+END_SRC" nil t)
(replace-match "#+BEGIN_SRC\\1#+END_SRC"))
(buffer-substring-no-properties (point-min) (point-max))))

(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."
Expand All @@ -386,6 +397,7 @@ This filter contains only subset of markdown syntax to be good enough."
(replace-regexp-in-string "^[[:space:]]*```$" "#+END_SRC")
(replace-regexp-in-string "^[[:space:]]*```" "#+END_SRC\n")
(replace-regexp-in-string "```" "\n#+END_SRC\n")
(ellama--replace-bad-code-blocks)
;; lists
(replace-regexp-in-string "^\\* " "+ ")
;; bold
Expand Down
28 changes: 28 additions & 0 deletions tests/test-ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,34 @@ 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-multiple-bad-blocks ()
(let ((result (ellama--translate-markdown-to-org-filter "Some text:
```
First block
```
other text:
```
Second block
```
more text:
```
third block
```
That's it.")))
(should (string-equal result "Some text:
#+BEGIN_SRC
First block
#+END_SRC
other text:
#+BEGIN_SRC
Second block
#+END_SRC
more text:
#+BEGIN_SRC
third block
#+END_SRC
That's it."))))

(provide 'test-ellama)

;;; test-ellama.el ends here

0 comments on commit 07b4597

Please sign in to comment.