(defun dojo-css-parse-is-main-css-resource (resource) (let* ((resource-file-path (dojo-resource-file-path resource))) (with-temp-buffer (buffer-disable-undo (current-buffer)) (insert-file-contents resource-file-path) (let* ((main-pos (re-search-forward "/\\*\\*\\*\\* DOJO-PROJECT-CSS-MAIN \\*/" nil t))) (not (null main-pos)))))) (defun dojo-css-parse-get-rules-by-class (resource css-class) (let* ((resource-file-path (dojo-resource-file-path resource)) (finished nil)) (with-temp-buffer (buffer-disable-undo (current-buffer)) (insert-file-contents resource-file-path) (while (not finished) (let* ((pos (search-forward (concat "." css-class)))) (if (null pos) (setq finished t) ))))) (list "a" "b")) (defun dojo-css-parse-extract-resource-manually (resource) "Test function to debug separate calls to that function." (interactive) (setf (dojo-workspace-curr-priority dojo-current-workspace) 1) (dojo-css-parse-extract-resource resource) (dojo-core-save-save-touched-classes dojo-current-workspace t) (let* ((resource-projects-to-save (dojo-workspace-resource-projects-to-save dojo-current-workspace)) (project-and-path (dojo-get-project-and-path buffer-file-name)) (project (nth 0 project-and-path))) (puthash project t resource-projects-to-save) (dojo-core-save-save-resource-files dojo-current-workspace)) (setf (dojo-workspace-curr-priority dojo-current-workspace) nil)) (defun dojo-css-parse-extract-resource (resource) (log-css (format "About to extract css resource [%s:%s:%s]" (if resource (dojo-resource-id resource) "---") (if resource (dojo-resource-project resource) "---") (if resource (dojo-resource-path resource) "---"))) (let* ((resource-file-path (dojo-resource-file-path resource)) (finished nil) (resource-id (dojo-resource-id resource)) (project (dojo-resource-project resource)) (path (dojo-resource-path resource)) (file-path (dojo-resource-file-path resource)) (prev-buffer (current-buffer)) (current-buffer-resource (dojo-core-workspace-get-or-create-resource (current-buffer)))) (if (and file-path (file-readable-p file-path)) (with-temp-buffer (buffer-disable-undo (current-buffer)) (if (and current-buffer-resource (eq (dojo-resource-id current-buffer-resource) (dojo-resource-id resource))) (insert-buffer-substring prev-buffer) (insert-file-contents file-path)) (dojo-css-parse-extract-current-buffer resource)) (log-css (format "[WARNING] dojo-css-parse-extract-resource does not find a file-path for resource-id [%s], project [%s] and path [%s]" resource-id project path))))) (defun dojo-css-parse-extract-current-buffer (resource) (let* ((size (buffer-size (current-buffer))) ; Up to here assumption: Unchanged buffer. (css-file (dojo-css-parse-buffer-modifying resource))) (setf (dojo-resource-last-parsed-utc-seconds resource) (float-time)) (setf (dojo-resource-last-parsed-size resource) size) (setf (dojo-resource-parsed-id resource) (if (dojo-css-file-p css-file) (dojo-css-file-id css-file) nil)) (setf (dojo-resource-state resource) (if (null css-file) "parse-failed" "parsed")) (log-css (format "... resource has now state [%s]" (dojo-resource-state resource))) (if css-file (let ((resource-projects-to-save (dojo-workspace-resource-projects-to-save dojo-current-workspace)) (project (dojo-resource-project resource))) (dojo-core-save-register-css-file-for-save dojo-current-workspace css-file) ; Mark the project, such that the resources.xml will be saved lateron. (puthash project t resource-projects-to-save))))) (defun dojo-css-parse-buffer-modifying (resource) (interactive) ; Strip comments (let* ((comments ()) (rules ()) (resource-id (dojo-resource-id resource)) (project (dojo-resource-project resource)) (path (dojo-resource-path resource)) (css-file (construct-dojo-css-file dojo-current-workspace resource-id project path))) (let* ((next-comment-start (search-forward "/*" nil t))) (while (not (null next-comment-start)) (goto-char next-comment-start) (let* ((next-comment-end (search-forward "*/" nil t)) (actual-comment-start (- next-comment-start 2)) (actual-comment-end (if (not (null next-comment-end)) next-comment-end (point-max))) (comment (buffer-substring actual-comment-start actual-comment-end)) (first-space-index (string-match " " comment)) (extra-star-count (if (null first-space-index) nil (- first-space-index 2))) (name (if (not (null first-space-index)) (dojo-common-strings-trim (substring comment first-space-index (- (length comment) 2))) nil))) (delete-region actual-comment-start actual-comment-end) (goto-char actual-comment-start) (insert-char ?\s (length comment)) (log-css (format "Extracted comment [%s] with start [%s] and end [%s]" comment actual-comment-start actual-comment-end)) (log-css (format "... extra-star-count [%s], name [%s]" extra-star-count name)) (if name (push (construct-dojo-css-comment actual-comment-start actual-comment-end extra-star-count name) comments))) (setq next-comment-start (search-forward "/*" nil t)))) (setq comments (nreverse comments)) ; Extract all rules (goto-char (point-min)) (let* ((rule-start (dojo-css-find-next-non-whitespace)) (curr-comment-index -1) (prev-comment-index curr-comment-index) (prev-extra-star-count -1) (curr-section-indices ())) (while (not (null rule-start)) (while (< (dojo-css-get-nth-comment-start-pos (1+ curr-comment-index) comments) rule-start) (incf curr-comment-index)) (if (> curr-comment-index prev-comment-index) (let* ((curr-extra-star-count (dojo-css-get-nth-comment-extra-star-count curr-comment-index comments))) (if (>= curr-extra-star-count 1) (progn (if (> curr-extra-star-count prev-extra-star-count) (push curr-comment-index curr-section-indices) (setq curr-section-indices (list curr-comment-index))) (setq prev-extra-star-count curr-extra-star-count))))) (setq prev-comment-index curr-comment-index) (log-css (format "Current comment index [%s] with content [%s]" curr-comment-index (dojo-css-get-nth-comment-content curr-comment-index comments))) (log-css (format "... with prev-extra-star-count [%s], curr-section-indices [%s]" prev-extra-star-count curr-section-indices)) (goto-char rule-start) (let* ((rule-end (search-forward "}" nil t)) (rule (if (not (null rule-end)) (buffer-substring (1- rule-start) rule-end) nil))) (log-css (format "... for rule [%s]" rule)) (push (construct-dojo-css-rule rule (reverse curr-section-indices)) rules)) (setq rule-start (dojo-css-find-next-non-whitespace))) (log-css (format "Parsed rules: [%s]" (reverse rules))) ; Each element of the returned list is an individual CSS rule. ; (assumption: the file is syntactically correct) (setq rules (nreverse rules))) (setf (dojo-css-file-comments css-file) comments) (setf (dojo-css-file-rules css-file) rules) css-file)) (defun dojo-css-find-next-non-whitespace () (re-search-forward "[[:alnum:]|\\.|{|}|:]" nil t)) (defun dojo-css-get-nth-comment-start-pos (index comments) (let* ((comment (if (and (not (null index)) (< index (length comments))) (nth index comments) nil)) (comment-start-pos (if comment (dojo-css-comment-start-pos comment) (1+ (point-max))))) comment-start-pos)) (defun dojo-css-get-nth-comment-extra-star-count (index comments) (let* ((comment (if (and (not (null index)) (< index (length comments))) (nth index comments) nil)) (comment-extra-star-count (if comment (dojo-css-comment-extra-star-count comment) (1+ (point-max))))) comment-extra-star-count)) (defun dojo-css-get-nth-comment-content (index comments) (let* ((comment (if (and (not (null index)) (< index (length comments))) (nth index comments) nil)) (comment-content (if comment (dojo-css-comment-content comment) nil))) comment-content)) ;.(defun dojo-css-parse-get-rule-at-point () ; (let* ((prev-comment-start (save-excursion (search-backward "/*" nil t))) ; (prev-comment-end (save-excursion (search-backward "*/" nil t))) ; (prev-comma (save-excursion (search-backward "," nil t))) ; (prev-closing-curly-bracket nil)) ; (while (> (point) prev-closing-curly-bracket ; ; ; ;(save-excursion (search-backward "*/" nil t))) ; ) (provide 'dojo-css-parse)