public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Need a little help writing a (not so easy?) filter…
@ 2015-04-01 17:04 Norman Markgraf
       [not found] ` <3f5205dd-8ef3-40b8-8dfc-8d8155470bc6-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: Norman Markgraf @ 2015-04-01 17:04 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


[-- Attachment #1.1: Type: text/plain, Size: 4413 bytes --]

Dear list-members,

I like to write a filter which would help me a lot with my (beamer) 
presentations. Currently a markdown like

~~~
#### Satz {.theorem}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
incidunt ut labore et dolore magna aliqua. 

####  Definition {.definition}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
incidunt ut labore et dolore magna aliqua. 

#### Beispiel {.example}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
incidunt ut labore et dolore magna aliqua. 

#### Block 

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
incidunt ut labore et dolore magna aliqua. 

~~~

will produce something similar to

~~~

\section{Header 1}\label{header-1}

\subsection{Header 2}\label{header-2}

\begin{frame}{Header 3}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\begin{block}{Satz}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{block}

\begin{block}{Definition}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{block}

\begin{block}{Beispiel}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{block}

\begin{block}{Block}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{block}

\end{frame}


~~~

I would like to change this behavior with a filter, so I can choose the 
type of block depending on the {.xxx} statement. The output shout be 
something similar to

~~~
\section{Header 1}\label{header-1}

\subsection{Header 2}\label{header-2}

\begin{frame}{Header 3}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\begin{theorem}{Satz}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{therorem}

\begin{definition}{Definition}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{definition}

\begin{example}{Beispiel}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{example}

\begin{block}{Block}

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
tempor incidunt ut labore et dolore magna aliqua.

\end{block}

\end{frame}

~~~

I tried something like

~~~{.haskell}

import Text.Pandoc.JSON

specialHeader :: Block -> Block
specialHeader (Header n attr@(_,classes,_) xs) | (n == 4) && (elem 
"theorem" classes) = Para ( RawInline (Format "latex") "\\begin{theorem}{": 
xs ++ [RawInline (Format "latex") "}"] )
specialHeader (Header n attr@(_,classes,_) xs) | (n == 4) && (elem 
"example" classes) = Para ( RawInline (Format "latex") "\\begin{example}{": 
xs ++ [RawInline (Format "latex") "}"] )
specialHeader (Header n attr@(_,classes,_) xs) | (n == 4) && (elem 
"definition" classes) = Para ( RawInline (Format "latex") 
"\\begin{definition}{": xs ++ [RawInline (Format "latex") "}"] )
specialHeader x = x


main :: IO ()
main = toJSONFilter specialHeader
     
~~~

but as you can see easily, the (correct} closing-tag "\end{….}" is missing. 
Since I am not a pro in haskel nor in the deeper pandoc api. Can some one 
please give me a hint, where and how to put the correct corresponding 
closing-tag? 

Thanks in advance

Norman Markgraf

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/3f5205dd-8ef3-40b8-8dfc-8d8155470bc6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 6357 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Need a little help writing a (not so easy?) filter…
       [not found] ` <3f5205dd-8ef3-40b8-8dfc-8d8155470bc6-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-04-03 14:20   ` Nick Yakimov
       [not found]     ` <012e82ce-c8a3-486a-848c-e6e4ba951d1c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2015-04-08  7:37   ` Norman Markgraf
  1 sibling, 1 reply; 6+ messages in thread
From: Nick Yakimov @ 2015-04-03 14:20 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


[-- Attachment #1.1: Type: text/plain, Size: 5947 bytes --]

The problem here is that headers and environments have different semantics, 
since environments enclose all elements, while headers are there by 
themselves. You could use Pandoc.Shared.hierarchicalize 
<https://hackage.haskell.org/package/pandoc-1.9.3/docs/Text-Pandoc-Shared.html#v:hierarchicalize> 
to get a hierarchy of sections, but this function returns [Element], and 
not [Block], so you'll have to convert back to [Block] by-hand.

import Text.Pandoc.JSON
import Text.Pandoc.Shared

specialHeader :: Pandoc -> Pandoc
specialHeader (Pandoc meta blocks) = Pandoc meta (elemToBlock 
$ hierarchicalize blocks)

elemToBlock :: [Element] -> [Block]
elemToBlock (Blk b:xs) = b ++ elemToBlock xs
-- do your pattern-matching here
elemToBlock (Sec lvl num attributes label contents:xs) = Header lvl 
attributes label ++ elemToBlock contents ++ elemToBlock xs

This is untested, but general idea should be applicable.

Another option would be to use enclosing divs for this, e.g.

<div class="theorem">
#### Satz

Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
incidunt ut labore et dolore magna aliqua. 
</div>

среда, 1 апреля 2015 г., 20:04:02 UTC+3 пользователь Norman Markgraf 
написал:
>
> Dear list-members,
>
> I like to write a filter which would help me a lot with my (beamer) 
> presentations. Currently a markdown like
>
> ~~~
> #### Satz {.theorem}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
> incidunt ut labore et dolore magna aliqua. 
>
> ####  Definition {.definition}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
> incidunt ut labore et dolore magna aliqua. 
>
> #### Beispiel {.example}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
> incidunt ut labore et dolore magna aliqua. 
>
> #### Block 
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor 
> incidunt ut labore et dolore magna aliqua. 
>
> ~~~
>
> will produce something similar to
>
> ~~~
>
> \section{Header 1}\label{header-1}
>
> \subsection{Header 2}\label{header-2}
>
> \begin{frame}{Header 3}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \begin{block}{Satz}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{block}
>
> \begin{block}{Definition}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{block}
>
> \begin{block}{Beispiel}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{block}
>
> \begin{block}{Block}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{block}
>
> \end{frame}
>
>
> ~~~
>
> I would like to change this behavior with a filter, so I can choose the 
> type of block depending on the {.xxx} statement. The output shout be 
> something similar to
>
> ~~~
> \section{Header 1}\label{header-1}
>
> \subsection{Header 2}\label{header-2}
>
> \begin{frame}{Header 3}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \begin{theorem}{Satz}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{therorem}
>
> \begin{definition}{Definition}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{definition}
>
> \begin{example}{Beispiel}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{example}
>
> \begin{block}{Block}
>
> Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod
> tempor incidunt ut labore et dolore magna aliqua.
>
> \end{block}
>
> \end{frame}
>
> ~~~
>
> I tried something like
>
> ~~~{.haskell}
>
> import Text.Pandoc.JSON
>
> specialHeader :: Block -> Block
> specialHeader (Header n attr@(_,classes,_) xs) | (n == 4) && (elem 
> "theorem" classes) = Para ( RawInline (Format "latex") "\\begin{theorem}{": 
> xs ++ [RawInline (Format "latex") "}"] )
> specialHeader (Header n attr@(_,classes,_) xs) | (n == 4) && (elem 
> "example" classes) = Para ( RawInline (Format "latex") "\\begin{example}{": 
> xs ++ [RawInline (Format "latex") "}"] )
> specialHeader (Header n attr@(_,classes,_) xs) | (n == 4) && (elem 
> "definition" classes) = Para ( RawInline (Format "latex") 
> "\\begin{definition}{": xs ++ [RawInline (Format "latex") "}"] )
> specialHeader x = x
>
>
> main :: IO ()
> main = toJSONFilter specialHeader
>      
> ~~~
>
> but as you can see easily, the (correct} closing-tag "\end{….}" is 
> missing. Since I am not a pro in haskel nor in the deeper pandoc api. Can 
> some one please give me a hint, where and how to put the correct 
> corresponding closing-tag? 
>
> Thanks in advance
>
> Norman Markgraf
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/012e82ce-c8a3-486a-848c-e6e4ba951d1c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 8053 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Need a little help writing a (not so easy?) filter…
       [not found]     ` <012e82ce-c8a3-486a-848c-e6e4ba951d1c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-04-03 14:25       ` Nick Yakimov
  0 siblings, 0 replies; 6+ messages in thread
From: Nick Yakimov @ 2015-04-03 14:25 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


[-- Attachment #1.1: Type: text/plain, Size: 976 bytes --]

Correcting myself, I should have used cons (:) instead of append (++) in 
some places there, and there obviously should be elemToBlock [] = []

elemToBlock :: [Element] -> [Block]
elemToBlock (Blk b:xs) = b : elemToBlock xs
-- do your pattern-matching here
elemToBlock (Sec lvl num attributes label contents:xs) = Header lvl 
attributes label : elemToBlock contents ++ elemToBlock xs
elemToBlock [] = []

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/e4275d6e-7636-4a54-a1ae-45e843f70460%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 1426 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Need a little help writing a (not so easy?) filter…
       [not found] ` <3f5205dd-8ef3-40b8-8dfc-8d8155470bc6-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2015-04-03 14:20   ` Nick Yakimov
@ 2015-04-08  7:37   ` Norman Markgraf
       [not found]     ` <5c9be25e-ca61-4694-a0bd-42893a0fb20c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  1 sibling, 1 reply; 6+ messages in thread
From: Norman Markgraf @ 2015-04-08  7:37 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


[-- Attachment #1.1: Type: text/plain, Size: 805 bytes --]

Thank you very much Nick Vakimov,

it helped a lot. I needed some time to figure out what to do, but now it 
works perfectly for me. 

Thanks,

Norman

PS: If someone needs my solution, just drop me a line an I will send it to 
you!

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/5c9be25e-ca61-4694-a0bd-42893a0fb20c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 1316 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Need a little help writing a (not so easy?) filter…
       [not found]     ` <5c9be25e-ca61-4694-a0bd-42893a0fb20c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2015-10-06 14:37       ` mickours-Re5JQEeQqe8AvxtiuMwx3w
  2015-10-07 11:56       ` Mohammed Haris Minai
  1 sibling, 0 replies; 6+ messages in thread
From: mickours-Re5JQEeQqe8AvxtiuMwx3w @ 2015-10-06 14:37 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1127 bytes --]

Hi Norman,

I'm interested in your filter. I think it should be the default behaviour.
.
I would like to know if it may work with reStructuredText and if yes what 
is the syntax?

Thanks,

Michael


Le mercredi 8 avril 2015 09:37:08 UTC+2, Norman Markgraf a écrit :
>
> Thank you very much Nick Vakimov,
>
> it helped a lot. I needed some time to figure out what to do, but now it 
> works perfectly for me. 
>
> Thanks,
>
> Norman
>
> PS: If someone needs my solution, just drop me a line an I will send it to 
> you!
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/7cace060-6f18-41aa-8847-ad0f892991bd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 1765 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Need a little help writing a (not so easy?) filter…
       [not found]     ` <5c9be25e-ca61-4694-a0bd-42893a0fb20c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  2015-10-06 14:37       ` mickours-Re5JQEeQqe8AvxtiuMwx3w
@ 2015-10-07 11:56       ` Mohammed Haris Minai
  1 sibling, 0 replies; 6+ messages in thread
From: Mohammed Haris Minai @ 2015-10-07 11:56 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1066 bytes --]

Hello Norman,

Could you please send this filter to me, I also use beamer quite a lot and 
this customization would make a lot more of beamer structure available.

On Wednesday, April 8, 2015 at 1:07:08 PM UTC+5:30, Norman Markgraf wrote:
>
> Thank you very much Nick Vakimov,
>
> it helped a lot. I needed some time to figure out what to do, but now it 
> works perfectly for me. 
>
> Thanks,
>
> Norman
>
> PS: If someone needs my solution, just drop me a line an I will send it to 
> you!
>

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/b6fff12e-1948-418c-b2b2-b53d9c4c827d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 1711 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-10-07 11:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-01 17:04 Need a little help writing a (not so easy?) filter… Norman Markgraf
     [not found] ` <3f5205dd-8ef3-40b8-8dfc-8d8155470bc6-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-04-03 14:20   ` Nick Yakimov
     [not found]     ` <012e82ce-c8a3-486a-848c-e6e4ba951d1c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-04-03 14:25       ` Nick Yakimov
2015-04-08  7:37   ` Norman Markgraf
     [not found]     ` <5c9be25e-ca61-4694-a0bd-42893a0fb20c-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2015-10-06 14:37       ` mickours-Re5JQEeQqe8AvxtiuMwx3w
2015-10-07 11:56       ` Mohammed Haris Minai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).