Coder Social home page Coder Social logo

swiftdocorg / markup Goto Github PK

View Code? Open in Web Editor NEW
112.0 5.0 9.0 108 KB

A Swift package for working with HTML, XML, and other markup languages, based on libxml2.

License: MIT License

Swift 98.22% Ruby 0.08% Makefile 0.21% C 1.49%
swift libxml2 html xml xpath xslt dtd relax-ng

markup's People

Contributors

compnerd avatar jessegrosjean avatar mattt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

markup's Issues

pointer being freed was not allocated: Error and workaround

I'm seeing pointer being freed was not allocated sometimes when trying to set attribute. In particular when trying to set attribute that's been removed from document and added to fragment like this:

let xml = #"""
    <item>
        <item a="1"/>
    </item>
"""#

let document = try XML.Document(string: xml)!
let three = document.search(xpath: "//*[@a=\"1\"]").first!
three.remove()
let fragment = DocumentFragment(children: { three })
three["a"] = "8"

I can avoid the crash if I create the document fragment like this instead:

let xmlDoc = document.rawValue.bindMemory(to: _xmlDoc.self, capacity: 1)
let fragment = DocumentFragment(rawValue: xmlNewDocFragment(xmlDoc))!

I guess it's bad to move and xml node belonging to a document to another node that doesn't belong to a document? Please let me know if you have a better idea of what's wrong and why the fix works.

possible bug: search on a node is executed on the whole DOM

I have a MusicXML document, in part-wise format, and I want to loop over all notes for each part.

<part id="P1">
    <measure number="1">
      <note>
        <pitch>
          <step>A</step>
        </pitch>
      </note>
    </measure>
    <measure number="2">
      <note>
        <pitch>
          <step>B</step>
        </pitch>
      </note>
    </measure>
    <measure number="3">
      <note>
        <pitch>
          <step>C</step>
        </pitch>
      </note>
    </measure>
</part>
<part id="P2">
    <measure number="1">
      <note>
        <pitch>
          <step>D</step>
        </pitch>
      </note>
    </measure>
    <measure number="2">
      <note>
        <pitch>
          <step>E</step>
        </pitch>
      </note>
    </measure>
    <measure number="3">
      <note>
        <pitch>
          <step>F</step>
        </pitch>
      </note>
    </measure>
</part>

when that document is loaded, and the following code is executed

 for part in document!.search(xpath: "//part") {
   debugPrint("Part:",part["id"] ?? "no part id found")
   for measure in part.search(xpath: "//measure") {
    var notes = ""
    for step in measure.search(xpath: "//step") {
      notes += step.text
     }
   debugPrint(notes)
  }
}

my expectation would be to get something like:

Part 1
ABC
Part 2
DEF

Instead I get

Part 1
ABCDEF
Part 2
ABCDEF

Memory management help

I know this lib isn't advertised as ready for production, but I've been having fun using it in a side project.

I've also been making some fixes as I run into issues:

#14

In my current use it seems the parsed libxml2 structures are never freed. I see that it's not as simple as calling xmlFree upon deinit of swift object since the xmlPtr's are not tied to swift object lifetimes. I'm unsure of best way to solve.

Do you have a memory management design in mind so that libxml2 structures are freed?

libxml2 for Xcode 12.5

I am using Xcode 12.5 on M1 Mac Big Sur 11.4 with swift 5.4

And after add this package, it gives the following error message

截屏2021-05-27 16 05 11

It seems that I did not have libxml2 setup properly, but the README says If you're on Linux or if you're on macOS and using Xcode < 11.4.

Anyway I install libxml2 using brew and try to force link it to fix this problem

$ brew install libxml2
$ brew link --force libxml2

But brew refused to do so

Warning: Refusing to link macOS provided/shadowed software: libxml2
If you need to have libxml2 first in your PATH, run:
  echo 'export PATH="/opt/homebrew/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/opt/homebrew/opt/libxml2/lib"
  export CPPFLAGS="-I/opt/homebrew/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/opt/homebrew/opt/libxml2/lib/pkgconfig"

So how can I solve this problem?

Can/Should Element insert(child:) be renamed to add(child:)

Is there a reason why this method isn't named add?

public func insert(child node: Node) {
    xmlAddChild(xmlNode, node.xmlNode)
}

The underlying API uses the term 'add'. To me insert means the method should take an index, or maybe the node will be put in the front of the children list.

great stuff thanks !

Hi i simply wanted to tell you that I find really useful what you are doing.
I am writing an app to process MusicXML format, and was really puzzling on how to proceed...
Thanks to your code I could very rapidly load the files and start doing the analysis I want, without investing any time in the very tedious XML parsing task..
It would be amazing when the XSLT support will be available .
Onnce you get to version 1.0 will be happy to write a tutorial / story on using your library for music .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.