From c998b937c14cb1f52b8a0ea40eb3ecb49c0c6f63 Mon Sep 17 00:00:00 2001
From: leungbk <bkleung89@gmail.com>
Date: Thu, 27 Dec 2018 01:09:32 -0800
Subject: [PATCH] Org writer: preserve line-numbering for example and code
 blocks.

---
 src/Text/Pandoc/Writers/Org.hs | 12 +++++++---
 test/command/5178.md           | 41 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 test/command/5178.md

diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs
index 12a54fd71..1ff34dd6f 100644
--- a/src/Text/Pandoc/Writers/Org.hs
+++ b/src/Text/Pandoc/Writers/Org.hs
@@ -188,13 +188,19 @@ blockToOrg (Header level attr inlines) = do
                   then empty
                   else cr <> nest (level + 1) (propertiesDrawer attr)
   return $ headerStr <> " " <> contents <> drawerStr <> blankline
-blockToOrg (CodeBlock (_,classes,_) str) = do
+blockToOrg (CodeBlock (_,classes,kvs) str) = do
   opts <- gets stOptions
   let tabstop = writerTabStop opts
+  let startnum = maybe "" (\x -> ' ' : trimr x) $ lookup "startFrom" kvs
+  let numberlines = if "numberLines" `elem` classes
+                      then if "continuedSourceBlock" `elem` classes
+                             then " +n" ++ startnum
+                             else " -n" ++ startnum
+                      else ""
   let at = map pandocLangToOrg classes `intersect` orgLangIdentifiers
   let (beg, end) = case at of
-                      []    -> ("#+BEGIN_EXAMPLE", "#+END_EXAMPLE")
-                      (x:_) -> ("#+BEGIN_SRC " ++ x, "#+END_SRC")
+                      []    -> ("#+BEGIN_EXAMPLE" ++ numberlines, "#+END_EXAMPLE")
+                      (x:_) -> ("#+BEGIN_SRC " ++ x ++ numberlines, "#+END_SRC")
   return $ text beg $$ nest tabstop (text str) $$ text end $$ blankline
 blockToOrg (BlockQuote blocks) = do
   contents <- blockListToOrg blocks
diff --git a/test/command/5178.md b/test/command/5178.md
new file mode 100644
index 000000000..9df3f6cf8
--- /dev/null
+++ b/test/command/5178.md
@@ -0,0 +1,41 @@
+```
+% pandoc -f rst -t org
+.. code:: haskell
+   :number-lines: 42
+
+   main = putStrLn "Hello World!"
+   unsafePerformIO main
+^D
+#+BEGIN_SRC haskell -n 42
+    main = putStrLn "Hello World!"
+    unsafePerformIO main
+#+END_SRC
+```
+
+```
+% pandoc -f org -t native
+#+BEGIN_SRC lisp -n 20
+    (+ 1 1)
+#+END_SRC
+
+#+BEGIN_SRC lisp +n 10
+    (+ 2 2)
+#+END_SRC
+^D
+[CodeBlock ("",["commonlisp","numberLines"],[("org-language","lisp"),("startFrom","20")]) "(+ 1 1)\n"
+,CodeBlock ("",["commonlisp","numberLines","continuedSourceBlock"],[("org-language","lisp"),("startFrom","10")]) "(+ 2 2)\n"]
+```
+
+```
+% pandoc -f native -t org
+[CodeBlock ("",["commonlisp","numberLines"],[("org-language","lisp"),("startFrom","20")]) "(+ 1 1)\n"
+,CodeBlock ("",["commonlisp","numberLines","continuedSourceBlock"],[("org-language","lisp"),("startFrom","10")]) "(+ 2 2)\n"]
+^D
+#+BEGIN_SRC lisp -n 20
+    (+ 1 1)
+#+END_SRC
+
+#+BEGIN_SRC lisp +n 10
+    (+ 2 2)
+#+END_SRC
+```