简体   繁体   中英

how to import a hash-table into an org-mode in emacs?

I have a hash-table and would like to export the hash-tables into an org-buffer. What the hash-table should print out to an org-buffer: take the keys, if the key's value is not a hash then it's ":: " otherwise if the key's value is a hash-table then the key is a heading and so on. Q. 1. I couldn't find if there is an 'import' into the org-buffer implemented already. If there is, can someone point me to it? 2. Has someone written anything similar to this? I can do it (it seems simple enough) but would hate to reinvent the wheel. If there is a library already out there that can take a structure (hash-table) and import it into an org-buffer, that would be awesome.

Thanks.

I have provided an example of the output of what that hash-table should be represented into the org-buffer and the raw hash-table.

* key "project-example" :id: "12345" ** affected-versions :id: "12332" :name: "SlimShady" :archived: nil :release-date: "2014-10-01T04:00:00.000Z" :released: nil :sequence: 81 :assigned-to: "m&m" :attach-name: nil ** components :id: "3214" :name: "Dr.Dre" :created: "2014-11-13T15:49:15.000Z" ** customer-fld-vals: :custom-fld-id: "cust-id-112233" :key: nil :values: "Fill me" :description: nil :duedate: nil :environment: nil :fixVersions: nil :key: "project-example" :priority: "high" :project: "EX" :reporter: "YourName" :resolution: "xx" :status: "xx" :summary: "Write something here" :type: "xx" :updated: "2014-11-15T22:52:13.000Z" :votes: 0

Raw-hash (i have a list which has only one hash in it):

((hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (id "12345" affected-versions #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (id "12332" name "SlimShady" archived nil release-date "2014-10-01T04:00:00.000Z" released nil sequence 81)) assigned-to "m&m" attach-name nil components #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (id "3214" name "Dr.Dre")) created "2014-11-13T15:49:15.000Z" customer-fld-vals #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8 data (customfieldId "cust-id-112233" key nil values ("Fill me"))) description nil duedate nil environment nil fixVersions nil key "project-example" priority "high" project "EX" reporter "YourName" resolution "xx" status "xx" summary "Write something here" type "xx" updated "2014-11-15T22:52:13.000Z" votes 0)))

I have stolen you all the fun;-). The output is a bit different to what you suggested. I do not indent the headers of the leaves so that org recognizes the structure.

(defun org-import-hash (title hash &optional level noindent)
  "Import HASH table into an org buffer.
Put TITLE into the first heading.
The hash table starts with LEVEL where LEVEL defaults to 1.
Indent inserted region by org-mode unless NOINDENT is non-nil."
  (interactive "sTitle:\nXHash-table:")
  (unless level (setq level 1))
  (let ((b (point)))
    (insert (if (or (looking-back "\n") (= (point) 1)) "" "\n") (make-string level ?*) (format " %s\n" title))
    (maphash (lambda (key val)
           (cond
        ((hash-table-p val)
         (org-import-hash key val (1+ level) t))
        ;; other special cases here
        (t
         (insert (format " :%s: %s\n" key val)))
        ))
         hash)
    (unless noindent
      (indent-region b (point)))
    ))

The comment section would not allow me extra characters so I couldn't post this code there.

Thank you for your solution, it's neat!!

I am new to elisp and do not know all the functions properly. Really like the 'make-string', my alternative is that I keep a list of stars per heeding and then concat them wherever necessary. I came up with solution shortly after i asked the question. I like your approach.

A few notes about my hash is that all the keys are symbols hence this: '(symbol-name k)'

(defun ->string (whatever)
  (cond
   ((equal 'integer (type-of whatever)) (number-to-string whatever))
   ((equal 'cons (type-of whatever)) (mapconcat 'identity (mapcar '->string whatever) " " ))
   ((equal 'string (type-of whatever)) whatever)))

(defun out->print (dstring)
  (print dstring))

(defun out->buffer (dstring)
  (insert dstring))

(defun print-issues (dhash stars)
  (maphash (lambda (k v)
             (progn (if (equal 'hash-table (type-of v))
                        (progn
                          (let ((nstars (cons "*" stars)))
                            (out->buffer  (concat (apply 'concat nstars) " " (symbol-name k) "\n"))
                            (print-issues v nstars)))
                      (out->buffer (concat (replace-regexp-in-string "\*" "\s"
                                                                     (apply 'concat stars))
                                           ":"
                                           (symbol-name k)
                                           ":"
                                           " -- "
                                           (->string v) "\n")))))
           dhash))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM