On 14-11-2011 09:47, Aditya Mahajan wrote:
On Fri, 11 Nov 2011, Steffen Wolfrum wrote:

quite often I wish we could have a kind of in line comment:
Something like ¿this is my comment¡ this.

I assume it is not trivial due to catcode issues, to say the least.
But nevertheless, as it is getting close to christmas, I just wanted
to mention this idea here.

As long as there is no line break in the comment, the following lua
based solution works (inspired by m-translate)

\startluacode
local comments = {}
thirddata = thirddata or {}

thirddata.comments = comments

function comments.translate(s)
return string.gsub(s, "¿.-¡","")
end

local textlineactions = resolvers.openers.helpers.textlineactions
utilities.sequencers.appendaction(textlineactions,"after","thirddata.comments.translate")


function comments.enable()
utilities.sequencers.enableaction(textlineactions,"thirddata.comments.translate")

end

function comments.disable()
utilities.sequencers.disableaction(textlineactions,"thirddata.comments.translate")

end

comments.disable()
\stopluacode

\def\enablecomments {\ctxlua{thirddata.comments.enable()}}
\def\disablecomments{\ctxlua{thirddata.comments.disable()}}

\appendtoks
\enablecomments
\to \everystarttext

\starttext
\section{An ¿in \iffalse comment¡ works}
Multiple ¿in \iffalse comment¡ with something ¿in \iffalse comment¡ in
betweeen
\stoptext

or ...

\startluacode

    thirddata          = thirddata or { }
    local comments     = { filters = { } }
    thirddata.comments = comments

    local textlineactions = resolvers.openers.helpers.textlineactions
    local sequencers      = utilities.sequencers
    local constants       = interfaces.constants

    local gsub, find, format = string.gsub, string.find, string.format

    function comments.install(name,settings)
        local settings = utilities.parsers.settings_to_hash(settings)
local start = string.escapedpattern(settings[constants.start] or "")
        local stop = string.escapedpattern(settings[constants.stop] or "")
        if start ~= "" and stop ~= "" then
            local pattern = start .. ".-" .. stop
            comments.filters[name] = function(s)
                if s == "" then
                    return ""
                elseif find(s,start) then
                    return (gsub(s,pattern,""))
                else
                    return s
                end
            end

sequencers.appendaction(textlineactions,"after",format("thirddata.comments.filters.%s",name))
            comments.disable(name)
        end
    end

    function comments.enable(name)

sequencers.enableaction(textlineactions,format("thirddata.comments.filters.%s",name))
    end

    function comments.disable(name)

sequencers.disableaction(textlineactions,format("thirddata.comments.filters.%s",name))
    end

\stopluacode

\unprotect

\unexpanded\def\installcomments{\dodoubleargument\doinstallcomments}

\def\doinstallcomments[#1][#2]%

{\ctxlua{thirddata.comments.install(\!!bs#1\!!es,\!!bs\detokenize{#2}\!!es)}}

\unexpanded\def\enablecomments[#1]%
  {\ctxlua{thirddata.comments.enable (\!!bs#1\!!es)}}

\unexpanded\def\disablecomments[#1]%
  {\ctxlua{thirddata.comments.disable(\!!bs#1\!!es)}}

\protect

\installcomments[test] [start=¿,stop=¡]
\installcomments[test2][start=¡,stop=¿]

\enablecomments[test]
\enablecomments[test2]

\starttext

\section{An ¿in \iffalse comment¡ works}
Multiple ¿in \iffalse comment¡ with something ¿in \iffalse comment¡ in betweeen

oeps ¿oeps¡ oeps ¡oeps¿ oeps

\stoptext



-----------------------------------------------------------------
                                          Hans Hagen | PRAGMA ADE
              Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
    tel: 038 477 53 69 | voip: 087 875 68 74 | www.pragma-ade.com
                                             | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the 
Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

Reply via email to