Purebasic -> TimelinePortable

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Syr2
Beiträge: 31
Registriert: 11.03.2020 13:39

Purebasic -> TimelinePortable

Beitrag von Syr2 »

Sollte jemand mit dem Programm Timeline Portable arbeiten und will mittels Purebasic eine XML erstellen um sie dort zu laden:

Link zu TimelinePortable: https://portableapps.com/node/38124
Der Code (mit Beispiel) exportiert die Struktur timeline in xml und überarbeitet nachträglich die Datei, weil das XML aus der Purebasic-lib und das XML von Timelineportable passt nicht zusammen.

Viel Spass beim langweilen,
Syr2

Code: Alles auswählen

Structure event
  start.s
  end.s
  text.s
  description.s
EndStructure
Structure displayed_period
  start.s
  end.s
EndStructure

Structure view
  displayed_period.displayed_period
  hidden_categories.s
EndStructure

Structure timeline
  version.s
  categories.s
  List events.event()
  view.view
EndStructure

Global timeline.timeline
Procedure.s FormatTime(day,month,year,hour,minute,second)
  Returnstring.s = ""
  Returnstring + Str(year)+"-"+Str(Month)+"-"+Str(day)+" "+Str(Hour)+":"+Str(Minute)+":"+Str(Second)
  ProcedureReturn Returnstring
EndProcedure
Procedure ModifyXmlFile(name.s)
  emptyxmlnodeRegex = CreateRegularExpression(#PB_Any,"<([^/<>]+)/>")
  
  content.s = ""
  f= OpenFile(#PB_Any,name)
  If Not f
    ProcedureReturn 
  EndIf
  While Eof(f) = 0
    content+ReadString(f)+#LF$
  Wend
  CloseFile(f)
  
  content = ReplaceString(content,"element>","event>")
  
  ExamineRegularExpression(emptyxmlnodeRegex,content)
  While NextRegularExpressionMatch(emptyxmlnodeRegex)
    replaceTag.s = RegularExpressionGroup(emptyxmlnodeRegex,1)
    content = ReplaceString(content,"<"+replaceTag+"/>","<"+replaceTag+"> "+"</"+replaceTag+">")
  Wend
  
  f= OpenFile(#PB_Any,name)
  WriteString(f,content)
  CloseFile(f)
  
EndProcedure

Procedure MakeTimeLine(Filename.s="")
  xml = CreateXML(#PB_Any)
  InsertXMLStructure(RootXMLNode(xml),timeline,timeline)
  If Filename = ""
    Filename.s = SaveFileRequester("Timeline speichern",GetCurrentDirectory()+"result.timeline","*.timeline",-1)
  EndIf
  SaveXML(xml,Filename)
  ModifyXmlFile(Filename)
EndProcedure


;Data to save
timeline\version = "0.12.1"
timeline\categories = ""
AddElement(timeline\events())
With timeline\events()
  \description = "Description"
  \start = FormatTime(01,01,2021,01,01,0)
  \end   = FormatTime(01,01,2021,01,01,0)
  \text  = "Test"
EndWith
timeline\view\displayed_period\start = FormatTime(01,01,2021,01,01,0)
timeline\view\displayed_period\end   = FormatTime(01,02,2021,01,01,0)

MakeTimeLine()
Syr2
Beiträge: 31
Registriert: 11.03.2020 13:39

Re: Purebasic -> TimelinePortable

Beitrag von Syr2 »

Hier mit etwas weniger Bugs und der Möglichkeit, Ordner rekursiv zu durchsuchen und die Ergebnisse mit Erstelldatum, Zugriffszeitpunkt und Änderungsdatum zu exportieren.

Code: Alles auswählen

Structure event
  start.s
  end.s
  text.s
  description.s
EndStructure
Structure displayed_period
  start.s
  end.s
EndStructure

Structure view
  displayed_period.displayed_period
  hidden_categories.s
EndStructure

Structure timeline
  version.s
  categories.s
  List events.event()
  view.view
EndStructure

Global timeline.timeline
Global Frame.POINT\x = Date()

Procedure.s FormatTime(day,month,year,hour,minute,second)
  Returnstring.s = ""
  Returnstring + Str(year)+"-"+Str(Month)+"-"+Str(day)+" "+Str(Hour)+":"+Str(Minute)+":"+Str(Second)
  ProcedureReturn Returnstring
EndProcedure
Procedure ModifyXmlFile(name.s)
  emptyxmlnodeRegex = CreateRegularExpression(#PB_Any,"<([^/<>]+)/>")
  
  content.s = ""
  f= OpenFile(#PB_Any,name)
  If Not f
    ProcedureReturn 
  EndIf
  While Eof(f) = 0
    content+ReadString(f)+#LF$
  Wend
  CloseFile(f)
  content = ReplaceString(content,"element>","event>")
  
  ExamineRegularExpression(emptyxmlnodeRegex,content)
  While NextRegularExpressionMatch(emptyxmlnodeRegex)
    replaceTag.s = RegularExpressionGroup(emptyxmlnodeRegex,1)
    content = ReplaceString(content,"<"+replaceTag+"/>","<"+replaceTag+"> "+"</"+replaceTag+">")
  Wend
  
  f= CreateFile(#PB_Any,name)
  WriteString(f,content)
  CloseFile(f)
  
EndProcedure

Procedure MakeTimeLine(Filename.s="")
xml = CreateXML(#PB_Any)
InsertXMLStructure(RootXMLNode(xml),timeline,timeline)
  If Filename = ""
    Filename.s = SaveFileRequester("Timeline speichern",GetCurrentDirectory()+"result.timeline","*.timeline",-1)
  EndIf
SaveXML(xml,Filename)
ModifyXmlFile(Filename)
EndProcedure

Procedure Crawl(Folder.s)
  d = ExamineDirectory(#PB_Any,folder,"*.")
  If Not IsDirectory(d)
    ProcedureReturn
  EndIf
  
  While NextDirectoryEntry(d)
    name$ = DirectoryEntryName(d)
    If name$ = "." Or name$ = ".."
      Continue
    EndIf
    If DirectoryEntryType(d) = #PB_DirectoryEntry_Directory
      Crawl(Folder+"\"+name$)
      Continue
    ElseIf DirectoryEntryType(d) = #PB_DirectoryEntry_File
      Date = GetFileDate(Folder+"\"+name$,#PB_Date_Created)
      If Date < 0 : Date = 0 : EndIf
      If date < frame\x And date > 0
        frame\x = date
      EndIf
      If date > frame\y
        frame\y = date
      EndIf
      AddElement(timeline\events())
      With timeline\events()
        \description = Folder+name$
        \start = FormatTime(Day(date),Month(date),Year(date),Hour(date),Minute(date),Second(date))
        \end   = FormatTime(Day(date),Month(date),Year(date),Hour(date),Minute(date),Second(date))
        \text  = "Created: "+name$
      EndWith
      
      Date = GetFileDate(Folder+"\"+name$,#PB_Date_Accessed)
      If Date < 0 : Date = 0 : EndIf
      AddElement(timeline\events())
      With timeline\events()
        \description = Folder+name$
        \start = FormatTime(Day(date),Month(date),Year(date),Hour(date),Minute(date),Second(date))
        \end   = FormatTime(Day(date),Month(date),Year(date),Hour(date),Minute(date),Second(date))
        \text  = "Accessed: "+name$
      EndWith
      
      Date = GetFileDate(Folder+"\"+name$,#PB_Date_Modified)
      If Date < 0 : Date = 0 : EndIf
      
      AddElement(timeline\events())
      With timeline\events()
        \description = Folder+name$
        \start = FormatTime(Day(date),Month(date),Year(date),Hour(date),Minute(date),Second(date))
        \end   = FormatTime(Day(date),Month(date),Year(date),Hour(date),Minute(date),Second(date))
        \text  = "Modified: "+name$
      EndWith
      
    EndIf
  Wend
EndProcedure

;Data to save
timeline\version = "0.12.1"
timeline\categories = ""

If CountProgramParameters() = 1
  Folder.s = ProgramParameter()
Else
  end
EndIf


Crawl(Folder.s)

timeline\view\displayed_period\start = FormatTime(Day(Frame\x),Month(Frame\x),Year(Frame\x),Hour(Frame\x),Minute(Frame\x),Second(Frame\x))
timeline\view\displayed_period\end   = FormatTime(Day(Frame\y),Month(Frame\y),Year(Frame\y),Hour(Frame\y),Minute(Frame\y),Second(Frame\y))

MakeTimeLine()
Antworten