Org-mode: เอาต์พุตบล็อกต้นทางไพพ์เป็น stdin ไปยังบล็อกซอร์สถัดไป


11

ฉันพยายามไพพ์เอาต์พุตของหนึ่งบล็อกแหล่งที่มากับบล็อกแหล่งถัดไปเป็นอินพุตมาตรฐาน นี่คือตัวอย่างของสิ่งที่ฉันมี:

Create stdin data:
#+header: :exports code
#+header: :results output
#+begin_src sh
echo "That goes to the next"
#+end_src

#+name: piped
#+RESULTS:
: That goes to the next 

Use "piped" as stdin:
#+header: :exports results
#+header: :stdin piped
#+header: :results output
#+begin_src sh
VALUE=$(cat)
echo "I got:"
echo "$VALUE"
#+end_src

ปัญหาของฉันคือ:

  • ฉันต้องสร้างผลลัพธ์ของบล็อกแรกด้วยตนเองโดยการกดปุ่ม C-c C-c

  • ผลลัพธ์จะต้องรวมอยู่ในบัฟเฟอร์องค์กร (ไม่จำเป็นต้องใช้เอาต์พุตขนาดใหญ่)

  • ผลลัพธ์จะต้องตั้งชื่อด้วยตนเอง

มีวิธีแก้ปัญหาหรือวิธีที่ดีกว่าในการทำเช่นนี้?

คำตอบ:


10

นี่คือวิธีง่ายๆในการแก้ไขโค้ดของคุณโดยตั้งชื่อบล็อก src แทนผลลัพธ์:

#+name: piped
#+header: :exports code
#+header: :results output
#+begin_src sh
echo "That goes to the next"
#+end_src

#+RESULTS:
: That goes to the next 

#+header: :exports results
#+header: :stdin piped
#+header: :results output
#+begin_src sh
VALUE=$(cat)
echo "I got:"
echo "$VALUE"
#+end_src

#+results:
: I got:
: That goes to the next

1
ดีมากขอบคุณที่ช่วยได้จริงๆ
theldoria

3

ฉันมีกรณีการใช้งานที่คล้ายกันและรีดผู้ส่งออกอย่างง่ายซึ่งให้ฉันใช้โหมด json สำหรับแหล่งที่มา / อินพุตจาก stdin:

;;; ob-passthrough.el ---  passthrough evaluator          -*- lexical-binding: t; -*-

;; this ob evaluates the block as ifself, so it can be used as input
;; for another block

(require 'ob)

(defun org-babel-execute:passthrough (body params)
  body)

;; json output is json
(defalias 'org-babel-execute:json 'org-babel-execute:passthrough)

(provide 'ob-passthrough)
;;; ob-passthrough.el ends here

จากนั้นเพิ่ม(passthrough . t)ไปยัง org-babel-list-langauges และนี่คือการดำเนินการ:

#+NAME: json-test
#+BEGIN_SRC json
  {"greet": "hello, world"}
#+END_SRC

#+HEADER: :stdin json-test
#+BEGIN_SRC sh
  jq .greet
#+END_SRC

#+RESULTS:
: hello, world

2

โทรบล็อก src จากที่อื่นโดยใช้การอ้างอิง "noweb" (ดู(info "(org) Noweb reference syntax")):

#+name: input
#+header: :exports code
#+header: :results output
#+begin_src sh
echo "That goes to the next"
#+end_src

#+header: :exports results
#+header: :results output :noweb no-export
#+begin_src sh
VALUE=$(<<input>>)
echo "I got:"
echo "$VALUE"
#+end_src

#+results:
: I got:
: That goes to the next

1
มันเจ๋งดีที่รู้ น่าเสียดายที่ซอร์สโค้ดบล็อกที่สองต้องใช้ stdin จริงๆ การใช้งานของcatเชลล์เป็นเพียงตัวอย่างง่ายๆ
theldoria

0

อีกวิธีหนึ่งในการแก้ปัญหานี้คือตั้งชื่ออินพุตว่า EXAMPLE หรือ QUOTE block หากอินพุตนั้นคงที่อย่างแท้จริง บางสิ่งเช่นนี้

#+NAME: some-json
#+BEGIN_QUOTE
{"label": "Hello json"}
#+END_QUOTE

หรือตัวอย่างถ้าคุณต้องการ:

#+NAME: some-json-2
#+BEGIN_EXAMPLE
{"label": "ehlo json"}
#+END_EXAMPLE

จากนั้นอ้างอิงบล็อกที่มีชื่อในรหัสที่คุณต้องการประเมิน ที่นี่เราใช้ตัวอย่าง QUOTE:

#+NAME: the-code
#+HEADER: :stdin some-json
#+BEGIN_SRC shell
jq .label
#+END_SRC

เนื่องจากค่าของsome-jsonบล็อกเป็นแบบคงที่จึงไม่จำเป็นต้องประเมินมัน การประเมินthe-codeบล็อกให้:

#+RESULTS: the-code
: Hello json
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.