adityam / filter

ConTeXt module to process contents of a start-stop environment through an external program
45 stars 10 forks source link

I can't read xml files #22

Closed ghost closed 9 years ago

ghost commented 9 years ago

With this filter:

\usemodule[filter]

\defineexternalfilter
    [python]
    [filtercommand={python3 \externalfilterinputfile\space > \externalfilteroutputfile}, location=text]

and this code:

import os.path

with open('./b.txt', 'r') as f:
    r = f.read()
    print(len(r))

all is fine except when b.txt is an XML file. I get this

references      > start problematic references

references      > unknown >    1: [][]

references      > stop problematic references

What happens?

adityam commented 9 years ago

Can you post a complete minimal working example with the XML file?

ghost commented 9 years ago

This is the contents of the b.xml. With other contents, I can't get any error:

<?xml version="1.0" standalone='yes'?>
<activitats>
  <activitat id="fitxes" tema="funcions" continguts="funció afí, dependència lineal, funcions a trossos">
    <presentacio tipus="tradicional" sintaxi="context" verbs="calcular">La fitxa blava i la fitxa vermella es mouen de la manera següent: la blava recorre les caselles d'una en una, mentre que la vermella les recorre de dues en dues però, després de cada moviment, s'espera un torn (figura~\in[]). Quina de les dues fitxes arribarà primer a la meta?
    </presentacio>
    <presentacio tipus="3-act" sintaxi="html" verbs="modelitzar">
     <actes>
       <u>vídeo fitxes</u>
       <dos></dos>
       <tres></tres>
     </actes>
    </presentacio>
  </activitat>
</activitats>
adityam commented 9 years ago

I don't have python3, but with python 2.7.8, the following works fine:

\usemodule[filter]

\traceexternalfilters

\defineexternalfilter
    [python]
    [filtercommand={python \externalfilterinputfile\space > \externalfilteroutputfile}, location=text,
    cache=no]

\startbuffer[data]
<?xml version="1.0" standalone='yes'?>
<activitats>
  <activitat id="fitxes" tema="funcions" continguts="funció afí, dependència lineal, funcions a trossos">
    <presentacio tipus="tradicional" sintaxi="context" verbs="calcular">La fitxa blava i la fitxa vermella es mouen de la manera següent: la blava recorre les caselles d'una en una, mentre que la vermella les recorre de dues en dues però, després de cada moviment, s'espera un torn (figura~\in[]). Quina de les dues fitxes arribarà primer a la meta?
    </presentacio>
    <presentacio tipus="3-act" sintaxi="html" verbs="modelitzar">
     <actes>
       <u>vídeo fitxes</u>
       <dos></dos>
       <tres></tres>
     </actes>
    </presentacio>
  </activitat>
</activitats>
\stopbuffer

\savebuffer[list=data, file=b.xml, prefix=no]

\starttext
\startpython
import os.path

with open('./b.xml', 'r') as f:
    r = f.read()
    print(len(r))
\stoppython

\stoptext
ghost commented 9 years ago

Here not, with python2 and python3. I don't know why!

adityam commented 9 years ago

Can you post the complete log file that you get when running my example?

On Aug 4, 2015, 12:15 PM, at 12:15 PM, Xavier notifications@github.com wrote:

Here not, with python2 and python3. I don't know why!


Reply to this email directly or view it on GitHub: https://github.com/adityam/filter/issues/22#issuecomment-127662548

ghost commented 9 years ago

I push my contents here.

adityam commented 9 years ago

In the first example (in your original bug report) you use print(len(r)) but in the second example (the one in your repo) you use print(r). So, the entire contents of your xml file are input as if you had literally typed them in your tex file. This means that you are compiling a tex file:

\starttext
..... \in[] ....
\stoptext

The error message:

references      > start problematic references

references      > unknown >    1: [][]

references      > stop problematic references

is telling you that <empty string> reference was not found.

ghost commented 9 years ago

With this

\startpython
from bs4 import BeautifulSoup
import json, os.path

with open('./dades/activitats.xml', 'r') as f:
    r = f.read()
    print(len(r))

#   soup = BeautifulSoup(r, "xml")
#
#   for a in soup.find_all("activitat", {'id': 'fitxes'}):
#       for p in a.find_all("presentacio"):
#           print(p.text)

\stoppython

I obtained the same result (empty string). What can I do=?

adityam commented 9 years ago
  1. Remove the .tuc file and try again.
  2. If that does not work, remove the .tuc file and add \traceexternalfilters. This will not remove the \jobname-temp-*.tmp file. Open this file in an editor to see that it is written correctly. Run it through python to ensure that you are getting correct output.

On Sat, 8 Aug 2015, Xavier wrote:

With this

\startpython
from bs4 import BeautifulSoup
import json, os.path

with open('./dades/activitats.xml', 'r') as f:
  r = f.read()
  print(len(r))

# soup = BeautifulSoup(r, "xml")
#
# for a in soup.find_all("activitat", {'id': 'fitxes'}):
#     for p in a.find_all("presentacio"):
#         print(p.text)

\stoppython

I obtained the same result (empty string). What can I do=?


Reply to this email directly or view it on GitHub: https://github.com/adityam/filter/issues/22#issuecomment-128977977

ghost commented 9 years ago

add \traceexternalfilters where?

adityam commented 9 years ago

See the example that I had posted.

On Aug 8, 2015, 10:35 AM, at 10:35 AM, Xavier notifications@github.com wrote:

add \traceexternalfilters where?


Reply to this email directly or view it on GitHub: https://github.com/adityam/filter/issues/22#issuecomment-128987366

ghost commented 9 years ago

I do not understand nothing.

ghost commented 9 years ago

Let's do simply example:

\startpython
import yaml

print("\startitemize")

with open('dades/accions.yaml', 'r') as f:
    r = yaml.load(f)
    print(r)
    for a in r:
        print("\item {0}".format(a))

print("\stopitemize")

\stoppython

This gives me blank string. How can I modify it for proper printing?

Thanks,

adityam commented 9 years ago

Does this run correctly if you save the contents as a python file and compile it?

On Aug 9, 2015, 5:38 AM, at 5:38 AM, Xavier notifications@github.com wrote:

Let's do simply example:

\startpython import yaml

print("\startitemize")

with open('dades/accions.yaml', 'r') as f: r = yaml.load(f) print(r) for a in r: print("\item {0}".format(a))

print("\stopitemize")

\stoppython

This gives me blank string. How can I modify it for proper printing?

Thanks,


Reply to this email directly or view it on GitHub: https://github.com/adityam/filter/issues/22#issuecomment-129145168

ghost commented 9 years ago

Yes

adityam commented 9 years ago

I don't know why this is not working. Can you create a complete and minimal example that does not work (similar to the one that I created in my comment above

ghost commented 9 years ago

I don't know too. With json it works

ghost commented 9 years ago

Ruby does not do that. Perfect with ruby.