Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
BostX committed Oct 30, 2021
1 parent 0261398 commit 7573b0b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 127 deletions.
5 changes: 1 addition & 4 deletions main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ E.g.:
racket main.rkt \\
-f '\\'(\"/home/bost/dev/notes/org-roam/20210805195404-guix.org\"
\"/home/bost/dev/notes/org-roam/20210729234518-racket.org\")' \\
-p title
"
-p title"
#:once-each
;; TODO parameterize displayed color
;; TODO check if the case-sensitivity value is allowed
Expand All @@ -92,7 +91,6 @@ racket main.rkt \\
;; Thanks to https://stackoverflow.com/q/16266934 for a hint
(define ns (namespace-anchor->namespace a))
(define (files) (eval (call-with-input-string (files-prm) read) ns))
#;(printf "~a\n" (files))

(define add-src-location-info #f)

Expand Down Expand Up @@ -134,7 +132,6 @@ racket main.rkt \\
(colorize lst ptrn)
(printf "\n\n")))
strs)))
#;(lambda (p) (printf "~s\n" p) p)
(curry map
(lambda (f)
(let ((strs (call-with-input-file f
Expand Down
36 changes: 5 additions & 31 deletions notes-reader.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,16 @@
(define note (parse-note src in))
(if (eof-object? note)
'()
(let* ((pn (parse-notes src in)))
#;(printf "note: ~a\n" note)
#;(printf "pn: ~a\n" pn)
#;(printf "cons: ~s\n" (cons note pn))
(cons note pn))))
(cons note
(parse-notes src in))))

(define (parse-note src in)
(regexp-try-match #px"^\\s+" in)
(if (eof-object? (peek-char in))
eof
(let ()
(define note-content (parse-note-content src in))
#;(printf "note-content: ~a\n" note-content)
note-content
;; `(note-content)
#;`(,note-content)
)))
note-content)))

(define (skip-whitespace in)
(regexp-try-match #px"^[ \t]+" in))
Expand All @@ -50,38 +43,21 @@
str))
(end (if positions (cdar positions) -1))
(lio (if positions (- end (string-length substr)) -1)))
#;(printf "(define positions ~s)\n" positions)
#;(printf "(define end ~s)\n" end)
#;(printf "last-index-of: ~s\n" lio)
lio))

(define (next-token-x src in (peek? #f))
(skip-whitespace in)
(define match-fn (if peek? regexp-match-peek regexp-try-match))
(cond
;; note is a single line:
#;((match-fn #rx"[^\n]*" in)
#;(match-fn #rx".*?\n" in)
=> (lambda (match)
(let ((fst (car match)))
(printf "next-token: string? ~a; bytes?: ~a; ~a\n" (string? fst) (bytes? fst) fst)
fst)))

;; note is a block of lines
((match-fn #rx".*?(\n\n|\n$|$)" in)
=> (lambda (match-raw)
(let* ((match (map
#;identity
bytes->string/utf-8 match-raw))
(let* ((match (map bytes->string/utf-8 match-raw))
(fst (car match))
(snd (cadr match)))
#;(printf "(define fst ~s)\n" fst)
#;(printf "(define snd ~s)\n" snd)
((compose
string->bytes/utf-8
#;(lambda (r) (printf "(define r ~s)\n" r) r)
(lambda (substr-index)
#;(printf "substr-index: ~a\n" substr-index)
(if (positive-integer? substr-index)
(substring fst 0 substr-index)
fst)))
Expand All @@ -98,9 +74,7 @@
(complain src in "unknown lexeme"))))

(define (next-token src in (peek? #f))
(let ((nt (next-token-x src in peek?)))
#;(printf "peek?: ~a; next-token: ~a~n" peek? nt)
nt))
(next-token-x src in peek?))

(define (parse-note-content src in)
(define first-token (next-token src in))
Expand Down
99 changes: 7 additions & 92 deletions notes.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,22 @@
racket/syntax
syntax/id-set))

;; Filter out lines containing given string. E.g. 'labor' returns lines 2, 3, 6
#|
1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
2 incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
3 nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
4 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
5 eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
6 in culpa qui officia deserunt mollit anim id est laborum.
|#

(define-syntax (notes form)
(syntax-parse form
((notes
text:string
case-sensitivity:string
note:expr ...)
(begin
#;(printf "(syntax->list #`(note ...)): ~a\n" (syntax->list #`(note ...)))
#;(printf "text: ~a\n" (syntax->datum #`text))
#;(printf "filter: ~a\n" (regexp (format "^.*~a.*"
(syntax->datum #`text))))
#`(begin
#;'(1 2 3 4)
#;'(#,@((compose
(curry map number->string)
(curry filter (lambda (n) (odd? n))))
'(1 2 3 4)))
#;(define f (compose
(curry map bytes->string/utf-8)
#;(curry filter (curry regexp-match
#;#px"^.*labor.*"
;; #rx"^.*labor.*"
(regexp (format "^.*~a.*"
(syntax->datum #`text)))
))
(curry map syntax->datum)))
'(#,@((compose
#;(lambda (r4) (printf "r4: list? ~s\n" (list? r4)) r4)
#;(lambda (r3) (printf "r3: ~s\n" r3) r3)
(curry map bytes->string/utf-8)
#;(lambda (r2) (printf "r2: ~s\n" r2) r2)
(curry filter (curry regexp-match
#;#px"^.*labor.*" #;#rx"^.*labor.*"
(regexp (format "(?~a:^.*~a.*)"
(syntax->datum #`case-sensitivity)
(syntax->datum #`text)))))
#;(lambda (r1) (printf "r1: ~s\n" r1) r1)
(curry filter
(curry regexp-match
#;#px"^.*labor.*" #;#rx"^.*labor.*"
(regexp (format
"(?~a:^.*~a.*)"
(syntax->datum #`case-sensitivity)
(syntax->datum #`text)))))
(curry map syntax->datum))
(syntax->list #`(note ...)))))))))

#|
(define t1
"aaa
bbb
ccc")
(define t2
"aaa
bbb
ccc")
(define t3
"aaa
bbb
ccc
")
(define t4
"aaaa
abbb
accc
baaa
bbbb
bccc
")
(define t
"1 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
2 incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
3 nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
4 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
5 eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
6 in culpa qui officia deserunt mollit anim id est laborum.
aaa
bbb
ccc
")
(regexp-match #rx".*?(\n\n|\n$|$)" t1)
;; => '("aaa\nbbb\n\n" "\n\n")
(regexp-match #rx".*?(\n\n|\n$|$)" t2)
;; => '("aaa\nbbb\nccc" "")
(regexp-match #rx".*?(\n\n|\n$|$)" t3)
;; => '("aaa\nbbb\nccc\n" "\n")
(regexp-match #rx".*?(\n\n|\n$|$)" t4)
|#

0 comments on commit 7573b0b

Please sign in to comment.