From 9cc9ed16fadeead0d846b2b43ef961791c74fe05 Mon Sep 17 00:00:00 2001
From: Igor <pashev.igor@gmail.com>
Date: Tue, 28 Dec 2010 22:34:13 +0300
Subject: Fixed export; example added

---
 bibtex.php    | 69 ++++++++++++++++++++++++++++++++---------------------------
 normalize.php | 14 ++++++++++++
 2 files changed, 51 insertions(+), 32 deletions(-)
 create mode 100644 normalize.php

diff --git a/bibtex.php b/bibtex.php
index 6979855..9fe5294 100644
--- a/bibtex.php
+++ b/bibtex.php
@@ -185,38 +185,6 @@ class BibtexParser
         return $res;
     }
 
-
-    /*
-     * Export sorted BiBTeX.
-     * Do not use after $this->expand_years()
-     *
-     */
-    public function export()
-    {
-        $res = '';
-        $this->sort();
-        foreach ($this->STRINGS as $key => $value)
-        {
-            $res .= '@STRING (' . $key . '="' . $value . '")' . "\n";
-        }
-
-        $res .= "\n\n";
-        
-        foreach ($this->ENTRIES as $ent)
-        {
-            $res .= '@' . mb_strtoupper($ent['entry']) . ' {' . $ent['id'] . ",\n";
-            $fields = array();
-            foreach($ent as $key => $value)
-            {
-                // Skip our special fields
-                if (!in_array($key, array('id', 'entry')))
-                    $fields[] = mb_strtoupper($key) . '=' . $value;
-            }
-            $res .= implode(",\n", $fields);
-            $res .= "\n}\n\n";
-        }
-        return $res;
-    }
 }
 
 
@@ -301,6 +269,43 @@ class BibtexParserTeam extends BibtexParser
         usort($this->SELECTION, array($this, 'cmp_entries'));
     }
 
+    public function sort_all()
+    {
+        usort($this->ENTRIES, array($this, 'cmp_entries'));
+    }
+
+    /*
+     * Export sorted BiBTeX.
+     * Do not use after $this->expand_years()
+     *
+     */
+    public function export()
+    {
+        $res = '';
+        $this->sort_all();
+        foreach ($this->STRINGS as $key => $value)
+        {
+            $res .= '@STRING (' . $key . '="' . $value . '")' . "\n";
+        }
+
+        $res .= "\n\n";
+        
+        foreach ($this->ENTRIES as $ent)
+        {
+            $res .= '@' . mb_strtoupper($ent['entry']) . ' {' . $ent['id'] . ",\n";
+            $fields = array();
+            foreach($ent as $key => $value)
+            {
+                // Skip our special fields
+                if (!in_array($key, array('id', 'entry')))
+                    $fields[] = mb_strtoupper($key) . '=' . $value;
+            }
+            $res .= implode(",\n", $fields);
+            $res .= "\n}\n\n";
+        }
+        return $res;
+    }
+
     protected function format_field_default($field)
     {
         $this->entry[$field] = $this->latex2html(
diff --git a/normalize.php b/normalize.php
new file mode 100644
index 0000000..836ef78
--- /dev/null
+++ b/normalize.php
@@ -0,0 +1,14 @@
+<?php
+require_once('bibtex.php');
+
+if (2 == $argc) {
+    $bibtex = new BibtexParserTeam();
+    $bibtex->read_file($argv[1]);
+    print $bibtex->export();
+} else {
+    print_r($argv);
+}
+
+
+?>
+
-- 
cgit v1.2.3