Skip to content

Instantly share code, notes, and snippets.

@mnbi
Created September 16, 2010 14:32
Show Gist options
  • Save mnbi/582519 to your computer and use it in GitHub Desktop.
Save mnbi/582519 to your computer and use it in GitHub Desktop.
#!/opt/local/bin/gosh
;; -*- coding: utf-8 -*-
;; get_tag_and_class.scm: extract tag and class names from a HTML document.
(define (match-tag-and-class line)
(rxmatch-case line
[#/<(\w+)[^<>]*class=[\'\"]([^\'\"]+)[\'\"][^<>]*>(.*)/
(all tag class rest)
(if (> (string-length rest) 0)
(append (list (cons tag class))
(match-tag-and-class rest))
(list (cons tag class)))
]
[else ()]))
(define (print-tag-and-class-with-number-inner lst num)
(if (not (null? lst))
(begin
(let ((top (car lst)))
(if top
(print num ": " (car top) "." (cdr top))))
(print-tag-and-class-with-number-inner (cdr lst) num))))
(define (print-tag-and-class-with-number lst num)
(if (not (null? lst))
(begin
(let ((result (car lst)))
(if (not (null? result))
(print-tag-and-class-with-number-inner result num)))
(print-tag-and-class-with-number (cdr lst) (+ num 1)))))
(define (print-tag-and-class lst)
(print-tag-and-class-with-number lst 1))
(print-tag-and-class
(map match-tag-and-class (port->string-list (standard-input-port))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment